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!