设置单个控制器的全局路由

时间:2013-05-25 13:40:39

标签: php symfony

我为单个捆绑包中的所有路由设置了/admin前缀,但我还想为此捆绑包内的单个控制器设置一些全局前缀。所以如果我创建了FooController,我可以写:

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

而不是:

@Route("/FooController/foomethod", name="/FooControler/foomethod")

有没有办法做到这一点,这被认为是一种很好的做法吗?

1 个答案:

答案 0 :(得分:1)

是的,这实际上是解决DRY事情的好方法(不要重复自己)。

您可以在控制器类上使用@Route注释指定所需的前缀,如下所示:

/**
 * @Route("/foocontroller")
 */
class FooController
{
    /**
     * @Route("/bar")
     */
     public function barAction()
     {
        // ... 
     }

    /**
     * @Route("/blub")
     */
     public function blubAction()
     {
        // ... 
     }

为你的barAction()和 / foocontroller / blub 导致blubAction()的 / foocontroller / bar

相关问题