Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering of routes using annotations

Tags:

symfony

The usual solution when you create routes in symfony and you want to have one route like

/{username}

so that it does not conflict with other routes like /login or /info is just to put that route as your last route in your routing.yml file. Since all the other routes take precedence this conflict is avoided. But how can you do this if you define your routes as annotations in your controllers? Is there any way to specify the ordering of this routes in this case?

like image 918
Carlos Granados Avatar asked Aug 01 '12 12:08

Carlos Granados


2 Answers

In the context of a controller, the order of action methods defines the order of routes. In the context of the whole application, you can import each controller explicitly to control the order, for example:

Home:
    resource: "\Vendor\Controller\HomeController"
    type: annotation

Security:
    resource: "\Vendor\Controller\SecurityController"
    type: annotation

security.log_out:
    pattern: "/logout"

User:
    resource: "\Vendor\Controller\UserController"
    type: annotation
like image 160
Elnur Abdurrakhimov Avatar answered Oct 31 '22 00:10

Elnur Abdurrakhimov


I can't comment on the answer, so I will leave how I had to write it in Symfony 2.3 to get it to work:

Home:
    resource: "@AcmeBundle/Controller/HomeController.php"
    prefix: /home #optional
    type: annotation

Notice the change of "\" to "/" and using .php at the end of controller name.

like image 27
Joel Avatar answered Oct 31 '22 01:10

Joel