One of the most common questions in Laravel world is how to organize the routes file, especially for bigger projects. Of course, you can have groups, prefixes and similar things, but today I want to present to you a package by Sebastiaan Luca – called Laravel Router.
Basically, this package allows you to group your routes into different files. As the author himself says:
“For instance admin, public, and user routes are separated into different classes instead of one long routes.php file”
You can find a detailed documentation on package GitHub page in Readme section, I will just summarize the main point – as a result, you create your “Routers” in app/Http/Routers folder, and here’s how one router file looks like:
namespace App\Http\Routers;
use SebastiaanLuca\Router\Routers\BaseRouter;
use SebastiaanLuca\Router\Routers\RouterInterface;
class PublicRouter extends BaseRouter implements RouterInterface
{
/**
* Map the routes.
*/
public function map()
{
$this->router->get('/', function () {
return 'Congratulations!';
});
}
}
Then you add all your “routers” into application’s Kernel class and that’s it. Within $this->router you can use all the same functions as in a common routes.php file.
What do you think? Useful package? If not, how do you structure your routes in bigger projects?
Link once again: Laravel Router
Ah, this is a nice idea, except we must register each Router to Kernel, maybe we can make it just include one router, and automatically will load all router?
Look so nice, will definitely try it out
Here’s how I do it: https://gist.github.com/kohenkatz/4348a9ebab7442a8867021641c7e6364
JW had a very good trick back from Lara v4, which u basically have a for-each loop that go through the files in ur routes folder and require them once one by one, so now ur workflow doesnt get effected by and u still use the same syntax as u were using the original route.php file 🙂