2013-07-01 54 views
17

我打电话渲染像这样在我的树枝模板(第18行)Symfony的控制器使嵌入错误:控制器URI“/ _fragment”不是调用

{{ render(controller('AcmeReadingBundle:Default:newAction')) }} 

而且控制器

public function newAction(Request $request) 
    { 
     $message = new Message(); 
     $form = $this->createFormBuilder($message) 
     ->add('body', 'text') 
     ->add('save', 'submit') 
     ->getForm(); 

     $form->handleRequest($request); 

     return $this->render('AcmeReadingBundle:Default:new.html.twig', array(
      'form' => $form->createView(), 
     )); 
    } 

而且new.html.twig文件

{{ form(form) }} 

我不断收到此错误:

An exception has been thrown during the rendering of a template ("The controller for URI "/_fragment" is not callable.") in AcmeReadingBundle:Default:show.html.twig at line 18. 

回答

30

解决方案:

你试图渲染使用controller()代替控制器/动作一个模板( '... new.html.twig')!

改变你的render功能:

{{ render(controller('AcmeReadingBundle:Default:new')) }} 

通知:在方法名称中没有 “......行动”)


提示:

的_fragment如果指定的控制器名称有问题,则大多会抛出异常。

即错误控制器/操作名称通常是此例外的原因。


延伸阅读:

看看this cookbook article

+0

我试过用这段代码和即时获得相同的结果 –

+0

对不起,应该是新的,而不是newAction – nifr

+0

我的控制器被称为newAction,如在代码示例。我称它为AcmeReadingBundle:默认:动作 –

0

我同意@nifr 您正在尝试使用controller()而不是控制器/操作呈现模板('... new.html.twig')!

变化渲染功能:

{{渲染(控制器( 'AcmeReadingBundle:默认:新'))}}

(注意:没有 “......行动” 的方法名)

Below is more if above solution is not giving you solution.

  1. 检查控制器中定义的功能是否是公共功能。在我的情况下,我已经定义了保护。

感谢,

Anirudh苏德。