简体   繁体   English

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

[英]Symfony 2: route defined in annotation not visible from by Twig's path()

I encountered a problem, have the following: 我遇到了一个问题,有以下几点:

DefaultController with a simple action: DefaultController有一个简单的动作:

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

In my twig template I try to use: 在我的树枝模板中,我尝试使用:

<form action="{{ path('register') }}" method="post"></form>

But I get the following error: 但是我收到以下错误:

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.

When I explicitely define a "register" route in app/config/routing.yml: 当我在app / config / routing.yml中明确定义“register”路由时:

register:
  pattern:  /register
  defaults: { _controller: EBTSCustomerBundle:Controller:Default:index }

Then it works fine. 然后它工作正常。 Can't find any reasonable docs about it, I thought that routes defined via annotations should be visible in the whole application. 找不到任何合理的文档,我认为通过注释定义的路径应该在整个应用程序中可见。

Any ideas guys? 有什么想法吗?

Routes by annotations still need to be imported into routing.yml as so: 注释路由仍然需要导入routing.yml,如下所示:

AcmeHelloBundle:
  resource: "@AcmeHelloBundle/Controller"
  type: annotation

This will tell the routing to scan the Controller directory of the Acme\\HelloBundle and import all routes. 这将告诉路由扫描Acme\\HelloBundleController目录并导入所有路由。

You can find more information about routing with annotations here . 您可以在此处找到有关使用注释进行路由的更多信息。 That link will also tell you how to activate routes as I have shown above. 该链接还将告诉您如何激活路线,如上所示。

Also, I noticed that your route annotation needs the name parameter to be accessible through register using the path function otherwise it'd be accessed through acme_bundlename_controllername_actionname : 另外,我注意到你的路径注释需要使用path功能通过register访问name参数,否则它将通过acme_bundlename_controllername_actionname访问:

@Route("/register", name="register")

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM