Symfony 2:注释中定义的路由在 Twig 的路径()中不可见

发布于 2024-12-20 00:08:22 字数 863 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2024-12-27 00:08:22

通过注释的路由仍然需要导入到routing.yml中,如下所示:

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

这将告诉路由扫描Acme\HelloBundleController目录并导入所有路由。

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

另外,我注意到您的路线注释需要使用 path 函数通过 register 访问 name 参数,否则将通过 <代码>acme_bundlename_controllername_actionname:

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

希望有帮助!

Routes by annotations still need to be imported into routing.yml as so:

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

This will tell the routing to scan the Controller directory of the Acme\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 through register using the path function otherwise it'd be accessed through acme_bundlename_controllername_actionname:

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

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文