Symfony 2:注释中定义的路径从Twig的路径中看不到()

mkr*_*arz 15 php routing annotations symfony twig

我遇到了一个问题,有以下几点:

DefaultController有一个简单的动作:

/**
 * @Route("/register")
 * @Template
 */
public function indexAction() {
    $oForm = $this->createForm(new RegisterType());
    return array(
        'form'  => $oForm->createView()
    );
}
Run Code Online (Sandbox Code Playgroud)

在我的树枝模板中,我尝试使用:

<form action="{{ path('register') }}" method="post"></form>
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

An exception has been thrown during the rendering of a template ("Route "register" does not exist.") in EBTSCustomerBundle:Default:index.html.twig at line 2.
Run Code Online (Sandbox Code Playgroud)

当我在app/config/routing.yml中明确定义"register"路由时:

register:
  pattern:  /register
  defaults: { _controller: EBTSCustomerBundle:Controller:Default:index }
Run Code Online (Sandbox Code Playgroud)

然后它工作正常.找不到任何合理的文档,我认为通过注释定义的路径应该在整个应用程序中可见.

有什么想法吗?

Kas*_*een 33

注释路由仍然需要导入routing.yml,如下所示:

AcmeHelloBundle:
  resource: "@AcmeHelloBundle/Controller"
  type: annotation
Run Code Online (Sandbox Code Playgroud)

这将告诉路由扫描Controller目录Acme\HelloBundle并导入所有路由.

您可以在此处找到有关使用注释进行路由的更多信息.该链接还将告诉您如何激活路线,如上所示.

另外,我注意到您的路径注释需要name通过register使用该path函数来访问该参数,否则它将通过acme_bundlename_controllername_actionname以下方式访问:

@Route("/register", name="register")
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!