Splitting the routing file

We just had this problem where our routing file for our application was getting bigger and bigger and huger and huger.

The same thing of course is happening to the file in which we define our schema. However it is already planned that you can split your schema file. Any file named *.schema.yml located at the same place as the initial one would be used as a schema.

However right now my issue was more with the routes. Even creating a Yaml category does not work, the file will not be parsed correctly.

This is not going to work although it would have been ok for me since then I could group things together and in my IDE it would be easy to view only the category I want. No luck here!

However in Symfony, Yaml files can always include php (as long as you are careful with the indentation). So you could use php to include files here. Which is just what I did.

Oh, … yeah! Before going further, note that a really better way would be to create plugins instead of having a massive application like we have.

The even better solution is to use symfony2 which solves this issue!

Now that you know what I’m doing is not the perfect solution, if you still want to read on, you’re welcome!

In apps/config, create a folder called routing. Inside that folder you are going to create as many Yaml routing files as you want. Give them names that represent what they are about like:

  • user.yml
  • product.yml
  • mapping.yml
  • ….

All the user related routes go into user.yml and are removed from the original routing.yml. In routing.yml I only keep some basic / general routes that don’t really belong to a big group. Here is what you should put into your routing.yml file in order for it to include all your routes:

Hope it helps some of you make things a bit more clean for their symfony projects. Also note that if you worry about performance of including routing files, you should remember that in production, this file is only parsed once and then is put in the cache so there isn’t much of an issue here!

2 thoughts on “Splitting the routing file

  1. Of course this is a valid solution and I don’t know your application. But that being said I find it much cleaner not to have any PHP in my yaml files.
    Instead I prefer a “separation of concerns” approach and split up functionalities into plugins. I.e. all commenting functionality go into myCommentingPlugin and all commenting related routes go to myCommentingPlugin/config/routing.yml.
    This way everything is still easy to maintain.

    • @Christian, you are absolutely completely incredibly right about that! In fact I even wrote in the article itself that creating plugins would be the better solution for this kind of matter.
      The fact is that the team I work with already has many difficulties understanding how symfony works. This is why it had been decided that we would not add one more layer (= the plugins), using Symfony in our case already represents a HUGE improvement over what existed. But we can’t change this many habits in the amount of time we had, so this is just better than nothing and keeps things at least a bit more organized.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>