Mirage
Mirage

Reputation: 31548

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. 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.

Upvotes: 8

Views: 8073

Answers (4)

George Mylonas
George Mylonas

Reputation: 722

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();
    }
....

Upvotes: 2

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44841

Use this in routing.yml:

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

Upvotes: 6

Mirage
Mirage

Reputation: 31548

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

Upvotes: 15

Vitalii Zurian
Vitalii Zurian

Reputation: 17976

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

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

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

Upvotes: 4

Related Questions