Laravel: Organising Your Route Files

By | July 22, 2018

Assalamualaikum / Hi,

Have you ever having hundred or probably thousand line of codes in a single routes/web.php ?

So, my solution is quite simple.

First thing, let’s look at this code snippet:

collect(glob(base_path('/routes/web/*.php')))
    ->each(function ($path) {
        require $path;
    });

So, it’s quite straightforward - the code above will simply require all files in the routes/web directory.

Means that, you can have infinite number of files in the directory.

All you need to do now, is to use the above code snippet for your web and api routes and please make sure to create web and api directory in routes directory.

Now you can move existing route file into respective folders and add as much files as you want in the directory.

Do checkout my route setup in Laravel Boilerplate.

Now, we have organised routes and no more hundred or thousand line of codes in single file!

Yeah!

Leave a Reply

Your email address will not be published. Required fields are marked *

seven + eighteen =