在Symfony 2中使用注释时生成URL的路由名称

zer*_*kms 5 php symfony

假设我有一个动作:

/**
 * @Route("/current")
 *
 * @return Response
 */
public function currentAction() 
{
}
Run Code Online (Sandbox Code Playgroud)

现在我需要为此操作生成url.$this->generateUrl()controller的方法接受路由名称作为参数.显然,只要我使用注释,我就没有这样的名字.

有没有解决方法呢?

zer*_*kms 15

得到它了:

/**
 * @Route("/current", name="foobar")
 *
 * @return Response
 */
public function currentAction() 
{
}
Run Code Online (Sandbox Code Playgroud)

通过阅读来源找到它,但实际上它也在文档中解释:http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#route-name

正如@Mihai Stancu所提到的 - 总有一个默认名称:

使用@Route注释定义的路由将被赋予一个默认名称,该名称由包名称,控制器名称和操作名称组成.

在这种情况下,它将是一个 bundlename_controllername_current

  • 在您提供的文档链接中:"使用@Route注释定义的路由被赋予一个默认名称,该名称由包名称,控制器名称和操作名称组成.对于上面的示例,这将是sensio_blog_post_index;". (2认同)