Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony routes: annotation vs file

What is better practise, to use annotations in Controllers or list of routes in file(s) (e.g. routes.yaml) and more importantly, Why?

I worked on big project, where you had all routes in yamls sorted by categories and every time you created new one, you had to update at least controller and some of files. I did not like it. I am hopefully starting one project and trying to decide what better option. I am considering using annotations, but I do not have enough experience yet to be sure it is right approach.

like image 475
L. Hilovska Avatar asked Sep 19 '25 13:09

L. Hilovska


2 Answers

In my opinion, both are good options, and there are not an absolute truth about it. You should use the one with which you feel most comfortable.

For me, the main difference between them are:

Annotations

  • Keep simple the process of read and update a route, since the route and controller are in the same file, very close to each other.

  • You're combining in the same file controllers and routing configurations.

YAML

  • More difficult to read; each time you need to check the route or params, you need to look for the correct yaml file.

  • More organised way and separated concepts.

My final preference is to go with annotations, the main reason for that is because I don't like the yaml format at all.

like image 84
Isaac Bosca Avatar answered Sep 21 '25 03:09

Isaac Bosca


It all depends

for common and simple routes to your AppBundle i suggest annotations, But for other bundles that you might want to reuse i like yaml, but the standard is xml. The reason is that the user of the third party bundle can copy the yaml/xml file, and place it in its appbundle and then he can change it and add his own version to his routing. A nice example is fosUserBundle. Imagine that you dont want a registration form because only the administrator may add new users. In that case you dont want routes to registration and would have to change route configuration

Dynamic routes

Sometimes you also need dynamic routes. SonataAdmin is a bundle that generates dynamic routes. It adds routes for each service that is tagged with sonata.admin.

like image 44
Frank B Avatar answered Sep 21 '25 01:09

Frank B