【问题标题】:Symfony controller embedd error: The controller for URI "/_fragment" is not callableSymfony 控制器嵌入错误:URI“/_fragment”的控制器不可调用
【发布时间】:2013-06-28 21:45:38
【问题描述】:

我在我的 Twig 模板(第 18 行)中这样调用渲染

{{ 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.

【问题讨论】:

    标签: symfony controller render


    【解决方案1】:

    解决方案:

    您正在尝试使用 controller() 而不是 controller/action 呈现 模板 ('...new.html.twig')!! p>

    将您的 render 函数更改为:

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

    注意:方法名称中没有“...Action”)


    提示:

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

    即控制器/动作名称拼写错误通常是导致此异常的原因。


    进一步阅读:

    看看this cookbook article

    【讨论】:

    • sorry 应该是 new 而不是 newAction
    • 我的控制器名为 newAction,如代码示例中所示。我把它叫做 AcmeReadingBundle:Default:Action
    • 还要注意在不同的捆绑包中有两个相同名称的控制器,并将您的操作放在错误的一个!在我的情况下,我有 AdminBundle:Tag 和 ContentBundle:Tag,我错误地将我的 listAction 放在了 AdminBundle 的控制器中。
    • 要考虑的另一件事:确保您的方法名称是methodNameAction,如果Action 关键字不存在,您将得到同样的错误。
    【解决方案2】:

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

    将渲染函数更改为:

    {{ 渲染(控制器('AcmeReadingBundle:Default:new')) }}

    (注意:方法名称中没有“...Action”)

    如果上面的解决方案没有给你解决方案,下面是更多。

    1. 检查控制器中定义的函数是否为公共函数。就我而言,我已经定义了受保护的。

    谢谢,

    阿尼鲁德·苏德。

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      相关资源
      最近更新 更多