EN— Laravel Routes Organization

In the last post

In case you have missed it, in my last post I mentioned several learning sources of information related to Laravel and other technologies, including podcasts, websites, facebook groups and Twitter profiles to follow. Here are the links:

Intro & Laravel Content References (PT-BR)

Laravel Routes

Today I will write about this one Laravel routes organization method which is being used by many people nowadays, its useful and I believe it fulfills its purpose well. Here we go.

At the side we find the default Laravel 5.1 folder structure. In this case, all routes are registered and loaded from the routes.php file.

The problem is: frequently this file becomes big, crowded with too many information and the bigger it gets the harder it becomes to work efficiently with it. If you don’t pay attention enough this file will make you waste some of your invaluable time. Lets see this simple example:

routes.php file example

In this routes.php file, we have nearly 100 lines of code , 3 route groups and our application is still far from being complete! It might seem silly, but in the future this will become even worse in terms of organization and friendliness to the eyes. So, what to do in order to avoid this situation?

Every person has its own style and ways to organize code and there is no right and wrong in this matter. So, what I am going to write here is merely an organizational convenience, a suggestion, and should not be granted as ‘the right way’ or anything like that. If you like it, I will be happy, if you don’t, please leave your comments and share your knowledge too. I hope this information help other people,

Organizing the routes

Why not organize routes with an intuitive name and put it inside a Routes folder? It might be a good idea creating a file for every route group, maybe adding a prefix like api_ ,admin_ or whatever feels appealing to you. As long as it makes sense and it is intuitive it is good in terms of organization! Think about that one day that you might need to do maintenance on that application and you will be glad that you did what you did with the route names.

Lets see the “how” part now. Did you notice that we still have the routes.php file in the image above? So, lets alter it in order to make it load all our routes from the Routes folder.


Summarizing, what is happening in the image is: we create a variable $route_partials to receive a list containing all our route file names which are stored inside the Routes folder(created by you). Right below, there is a for loop used to look for the route files listed in the route_partials variable in the Routes folder, require_once every one of them and we are good to go! Nice to note that the order matters in this case, because the require is made in order of declaration.

New Flow

From now on, you will need to follow this flow when creating new routes in your Laravel application.

  1. Create a file inside the Routes folder
  2. Add to your recently created file all routes-related stuff like you usually would do in routes.php
  3. Add the name of this new file to the $route_partials array in routes.php
Example of a file inside Routes folder


There is no downside in being a little bit more organized, this kind of stuff might help you save some time in the long run and also makes your project more appealing to the eyes. I hope you have learned something and liked this approach, please leave your opinion and you are welcome to share other ways to do it. Thanks for your time once again.