Imagine a Laravel project with 100+ routes, separate section for guest, users, administrators etc. Do you really want to keep it all in one file? How can you group them, add prefixes to URLs? Let’s see what options we have.
1. Separate WEB and API Routes
This one is easy, as Laravel is shipped with this feature by default. There are two files:
So if your project has both visual web-pages, and API (which is more and more common these days), please put API routes in that separate file.
For example, if you have /users page and then /api/users/ endpoint, separating them into their own files help to not get confused with the same names in the same file.
That said, I recently saw counter-intuitive example from official Laravel project. With Laravel Horizon, Taylor has only API routes, and he didn’t use separate file, instead he put it into routes/web.php:
Another proof that structuring in Laravel is very personal and there is no 100% standard, even from Taylor himself.
2. Structure routes/web.php File into Groups
That also comes from “basic” Laravel – route grouping. This is an example from the official Laravel documentation:
The most basic usage is hiding different groups under different middleware. For example, you want one group to be restricted by default auth middleware, another group by separate admin custom middleware etc.
With that, you can also use Route group names and prefixes. Again, a few examples from the official documentation:
Also, if you want to add all middleware+name+prefix to one group, it’s more readable to put them into an array:
Let’s tie it all together into a real-life example and three groups:
Here’s a way to group it all in routes/web.php file:
3. Grouping Controllers with Namespaces
In the example above, we didn’t use Controllers, we just returned static text as an example. Let’s add Controllers, with one more “twist” – we will structure them to the folders with their own different namespaces, like this:
And then we can use them in our Routes file:
But what if we have a lot of controllers in that group? Should we keep adding FrontSomeController all the time? Of course not. You can specify the namespace as one of the parameters, too.
4. Group within a Group
The situation above, with three groups, is simplified, real projects have a little different structure – of two groups: front and auth. And then inside of auth there are sub-groups: user and admin. For that, we can create sub-groups in routes/web.php and assign different middlewares/prefixes etc.
We can do it even with more than two levels, here’s an example from open-source project Akaunting:
Another example is from another popular Laravel CRM called Monica:
5. Global Settings in RouteServiceProvider
There is a file which serves for all routes settings – app/Providers/RouteServiceProvider.php. It has method map() where it binds both routes files – web and API:
Have you noticed middleware, namespace and prefix being mentioned in the methods? That’s where you can set the global settings for the whole file, so you wouldn’t have to repeat them for every Route group inside the file.
It’s mostly used for API routes, as their settings are usually the same, like this:
This method above will prefix all API URLs with api/v1/ in the beginning.
6. Grouping into More Files – is it worth it?
If you have huge amount of routes and want to group them even more, into separate files, then you can use the same file mentioned in the previous section – app/Providers/RouteServiceProvider.php. If you take a closer look at its map() methods, you will see commented out place at the end:
You can interpret it as kind of an “invitation” to add more files, if you wish. So you can create another method like mapAdminRoutes() inside this file, and then add it into the map() method, and your separate file will be registered and loaded automatically.
But, personally, I don’t see much advantage in this approach, and I haven’t seen it done very often. It brings a little more separation of routes, but sometimes you get lost in those files and not sure where to look for specific route.
7. Find Exact Route with Artisan route:list command
Speaking of bigger routes and getting lost there, we have one artisan command which helps to locate a certain route.
You probably all know that php artisan route:list will give you all the routes in the project:
But did you know you have more filter abilities to find the exact thing you want? Just add –method, or –name, or –path with parameters.
Filter by method – GET, POST etc:
Filter by name or URL part:
That’s all I could tell about grouping routes in bigger projects. Do you have any other examples? Please share in the comments.
This content was originally published here.