Nasrul Hazim Bin Mohamad

Manage Routes in Laravel

Oct
19

Assalamualiakum & Hi,

It’s obvious, if you’re working on a large application which have a lot of routes needed, you will absolutely need to manage your routes – for me mainly the web and API routes.

So here an example how I did – Create a helper to load multiple route in particular directory.

The second part basically just specify to which directories to load the routes.

Routes Directory Structure

Routes Directory Structure

This method inspired by Spatie – Laravel Collection Macros.

You may use macro as well to load multiple routes. It depends how you want to manage your routes. For me, this is good enough for me.

Laravel: Managing Routes

Dec
13

Assalamualaikum,

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:

  1. 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.
  2. 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.
  3. 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!