Through your development, I am sure that you have MANY MANY routes in your routes.php file.  Never fear, this happens to all of us.  I know by the end of my project, I have to go back through and clean up my routes file.  After a few months of the project being done and having to go back to the project to make some updates, I have to go through the routes.php file and figure out what I was doing and yell at myself for not being more organized.

Because of all of this, I have been working on a new way of keeping my routes file more organized.  I am a member of Laracasts which I would suggest everyone become a member and support what Jeff is doing, however in one of his videos, he talked about this same thing.  I have adapted his solution and made some updates myself.  The solution in the Laracasts video has you create files in a routes folder within /app.  This works great but has you have a bunch of files flat in that file.  What about wanting to keep things even more organized.  This is why I have extended this solution.  Here is the current solution I have come up with.

 

[php]

foreach (File::allFiles(__DIR__.’/routes’) as $partial)
{
if($partial->getRelativePath() != ”) {
Route::group([‘prefix’=>$partial->getRelativePath()], function() use ($partial) {
require $partial->getPathname();
});
} else {
require $partial->getPathname();
}
}

[/php]

 

Let me explain what this is doing.  I am getting a directory listing of all files in the app/routes folder.  From this I figure out if it is in a folder within the routes folder and make that a route prefix.  If the file is part of a prefix, i then include the file within the route group, otherwise i just include it outside of a prefix.

There are some things within here that I will improve but this is an initial update of how you can clean up your routes file.