简体   繁体   English

如何在Symfony2中为所有控制器添加一些路由前缀?

[英]How can I add some route prefix to all controllers in Symfony2?

I am using annotations to define routes in controllers and I have 15 controllers. 我正在使用注释来定义控制器中的路由,我有15个控制器。 All are executed by /path1 , /path2 . 全部由/path1/path2

Is there any way that in all those controller , I can access them via /admin/path1 and /admin/path2 ? 有没有办法在所有那些控制器中,我可以通过/admin/path1/admin/path2访问它们?

I don't want to enter that by changing each file. 我不想通过更改每个文件来输入它。

Can I do that from a single location? 我可以从一个地方做到吗? I mean the whole bundle should open via /admin and then their respective paths. 我的意思是整个包应该通过/admin然后它们各自的路径打开。

try this 试试这个

# app/config/routing.yml
acme_hello:
    resource: "@AcmeHelloBundle/Resources/config/routing.yml"
    prefix:   /admin

or if using annotations 或者如果使用注释

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

Use this in routing.yml : routing.yml使用它:

Admin:
    resource: "@AdminBundle/Controller"
    type: annotation
    prefix: /admin

Just define the annotation for your Class (not for method) 只需为您的类定义注释(不适用于方法)

/**
* @Route("/blog")
*/

http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#route-prefix http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#route-prefix

If you want to prefix specific controller DevController for example and have something like: 例如,如果您想为特定控制器DevController添加前缀,请执行以下操作:

myproject.com/dev/test myproject.com/dev/test

in your Controller add the following Route annotation as in example: 在您的Controller中添加以下Route注释,如下所示:

    /**
 * @Route("/dev")
 */
class DevController extends Controller{

    /**
     * @Route("/test")
     */
    public function testSavingAction(){

        return new Response();
    }
....

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

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