Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

symfony

I am using annotations to define routes in controllers and I have 15 controllers. All are executed by /path1 , /path2.

Is there any way that in all those controller , I can access them via /admin/path1 and /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.

like image 542
Mirage Avatar asked Jul 25 '12 06:07

Mirage


4 Answers

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
like image 197
Mirage Avatar answered Oct 08 '22 09:10

Mirage


Use this in routing.yml:

Admin:
    resource: "@AdminBundle/Controller"
    type: annotation
    prefix: /admin
like image 31
Elnur Abdurrakhimov Avatar answered Oct 08 '22 10:10

Elnur Abdurrakhimov


Just define the annotation for your Class (not for method)

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

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

like image 34
Vitalii Zurian Avatar answered Oct 08 '22 09:10

Vitalii Zurian


If you want to prefix specific controller DevController for example and have something like:

myproject.com/dev/test

in your Controller add the following Route annotation as in example:

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

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

        return new Response();
    }
....
like image 2
George Mylonas Avatar answered Oct 08 '22 11:10

George Mylonas