Hello!

At the moment I am studying Symfony 2 and this dilemma has arisen before me:

How best to store routes for controllers?

Yes, I understand that this question has a bit of holivar overtones, because the taste and color ... But still I am interested in this question from the point of view of organization of medium / large applications.

I will give my thoughts:

As far as I know, symfony has 2 main routes for storing routes

  1. In configs (routing.yml, routing.xml, etc.), it is also possible to connect routing from bundles.
  2. In the annotations

The variant with annotations seems to me to be the most convenient, but when there are a lot of controllers and methods, say 50+, there will be a difficulty in determining what is and where, and as a result, development and debagging complications.

It also seems acceptable to me to put routes into the routing files of each bundle. In this case, it will be convenient to reuse bandlets in other projects, and knowing what functionality a particular bundle is responsible for, look for the necessary routes. But still with a large number of bundles this can be a problem.

The third option is to store all the routes in app / config / routing.yml. But it will complicate the reuse of bundles. And, for some reason, I intuitively dislike this option.

From the side it may seem that I asked the question and answered it myself. This is not true. I see these options, but I don’t know which one to use and when, since there’s no experience with any serious Symfony development.

I will be glad and grateful to any opinions, and in twofold - to the opinion that is reasoned.

    2 answers 2

    I use symfony for two years, on 4 projects. In the first two I used configuring routes in a yaml file, on the third I decided to try annotations. I understood that the use of annotations is much more convenient, although at first I thought "how good is when all the routes are in one file." Just the opposite. At a minimum, when creating a new controller, you have to do something in two files, and with annotations - ctrl + c - ctrl + v in one file, slightly renamed and ready. If you strictly follow the naming of files and routes - there are no problems with finding the desired controller. Especially in the IDE, where just by the name of the route in the template poked and you're already in the controller.

    From the point of view of performance: on the first project, I actively smoked docks and recommendations with your similar question. Subtracted somewhere that yaml-files more productive. But in reality, there is no noticeable difference, everything is well cached anyway and is ready for use by the engine "at its best."

    I also add that the projects are large enough so that the differences in the use of approaches are noticeable. So with a yaml-file it is more unpleasant: a huge footcloth of 1.5k lines, in which both necessary and unnecessary and you still want to observe some order and divide the comments into sections so that it is more decent ... And with annotations everything is in its place, always handy, no porridge.

      Developers themselves at their Best Practice suggest using route configuration, caching, and security via annotations:

      Make it your controller for the controller.

      and I totally agree with them. It is very convenient when the configuration is in the same place with the code, and there is no difference in speed.

      In addition, using annotations for routing, caching and security simplifies configuration. There is no need for any other configuration.