Symfony 2:注释中定义的路由在 Twig 的路径()中不可见
我遇到了一个问题,有以下问题:
DefaultController 具有简单的操作:
/**
* @Route("/register")
* @Template
*/
public function indexAction() {
$oForm = $this->createForm(new RegisterType());
return array(
'form' => $oForm->createView()
);
}
在我的树枝模板中我尝试使用:
<form action="{{ path('register') }}" method="post"></form>
但出现以下错误:
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.
当我在 app/config/routing.yml 中明确定义“注册”路由时:
register:
pattern: /register
defaults: { _controller: EBTSCustomerBundle:Controller:Default:index }
然后效果很好。找不到任何有关它的合理文档,我认为通过注释定义的路由应该在整个应用程序中可见。
大家有什么想法吗?
I encountered a problem, have the following:
DefaultController with a simple action:
/**
* @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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过注释的路由仍然需要导入到routing.yml中,如下所示:
这将告诉路由扫描
Acme\HelloBundle
的Controller
目录并导入所有路由。您可以在此处找到有关使用注释进行路由的更多信息。该链接还将告诉您如何激活路线,如我上面所示。
另外,我注意到您的路线注释需要使用
path
函数通过register
访问name
参数,否则将通过 <代码>acme_bundlename_controllername_actionname:希望有帮助!
Routes by annotations still need to be imported into routing.yml as so:
This will tell the routing to scan the
Controller
directory of theAcme\HelloBundle
and import all routes.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 throughregister
using thepath
function otherwise it'd be accessed throughacme_bundlename_controllername_actionname
:Hope that helps!