Laravel: Managing Routes
Recently I’ve meet some of my friends, and clients working with Laravel projects. It’s seems that, Route, managing routes is quiet painful when your application become bigger and you have to handle a lots of routes.
So, what’s come out of my mind was, how to manage Laravel’s routes more efficient?
I’ve come up with few possible options:
- Define more map methods in RouteServiceProvider, and call them – but this will make the RouteServiceProvider full with map methods, which I don’t prefer it that way – to many mapping routes method.
- Then I’ve come up with ideas having route trait, then you just need to attach it to any classes which you prefer, then call it any RouteServiceProvider or in routes/web.php. But having trait just to manage the routes, it’s like overkill (up till now), so I skip creating the trait.
- Then I’ve come up with having another route folder, purposely to serve the routes – I’ve named it Routes, in app directory. In the Routes directory, you create a class, and have a method routes. In that method you just need to define your route – but of course, you need to import Route facade. Below is an example of implementation.
Now you should have much cleaner codes in your routes/web.php. You just need to include the route namespace, and call the className::routes() to load the routes.
That will be much cleaner routes. You may want to structure your app/Routes directory to support route’s version, or prefix, etc.
Hopes the idea will help out developers to easily manage their projects.
Hope the approach won’t break the incoming Laravel releases! ^_^
I think that’s what I want to share for now. Thanks for reading!