DEV Community

Cover image for Developing Quality API with Laravel - Tip #2
Hesam Rad
Hesam Rad

Posted on

Developing Quality API with Laravel - Tip #2

Tip #2 - Divide Major Route Groups in Specific Files

What do I mean by this?

Imagine a scenario where you are developing a complex API which has different consumers with different route groups.

It is 100% ok to keep all the routes in api.php file under routes directory; but you know me, I'm looking for a cleaner solution.

As a backend developer, I tend to spend some time searching through routes to do something like debugging, logging and all other sort of things.

It is going to get really painful to find the exact route you're looking for if you have +1000 routes, right?

What I would suggest to do is dividing each major group into their specific files and then include said files in your api.php file.

Let's see an example.

//This is routes/api.php

/*
 * Api version 1 routes.
 * 
 * All v1 routes of the application live here.
 * 
 * 
 */

Route::prefix('v1')->group(function () {

    /*
     * Authentication routes.
     * 
     * 
     */
     Route::prefix('auth')->group(function () {
        Route::get('/route-1', [SomeController::class, 'method-1']);
        Route::get('/route-2', [SomeController::class, 'method-2']);
        Route::get('/route-3', [SomeController::class, 'method-3']);
        Route::get('/route-4', [SomeController::class, 'method-4']);
        Route::get('/route-5', [SomeController::class, 'method-5']);
        //...
    });

    /*
     * Admin routes.
     * 
     * 
     */
     Route::prefix('admins')->group(function () {
        Route::get('/route-1', [AnotherController::class, 'method-1']);
        Route::get('/route-2', [AnotherController::class, 'method-2']);
        Route::get('/route-3', [AnotherController::class, 'method-3']);
        Route::get('/route-4', [AnotherController::class, 'method-4']);
        Route::get('/route-5', [AnotherController::class, 'method-5']);
        //...
    });
});
Enter fullscreen mode Exit fullscreen mode

It's seems ok now, right? But is it going to be ok two years from now? What about when there is a version 2 or 3 for this API?

I can see routes piling up and making it harder for us to work with them.

So what can we do to avoid a messy routes file and keep things clean?

This is what I do in my own projects.

I would create a folder inside routes folder and name it the same as the API version I'm developing.

- app
- bootstrap
- config
- database
- public
- resources
- routes
    + v1              <--- This is that folder
    + api.php
    + channels.php
    + console.php
    + web.php
- storage
- tests
Enter fullscreen mode Exit fullscreen mode

And then I would extract major route groups in their respective files.

Following the above example, I extract all authentication routes and store them in a new file called auth.php under routes/v1 folder that I just created.

//This is routes/v1/auth.php

/*
 * Authentication routes.
 * 
 * 
 */
 Route::namespace('Auth')->group(function () {
    Route::get('/route-1', [SomeController::class, 'method-1']);
    Route::get('/route-2', [SomeController::class, 'method-2']);
    Route::get('/route-3', [SomeController::class, 'method-3']);
    Route::get('/route-4', [SomeController::class, 'method-4']);
    Route::get('/route-5', [SomeController::class, 'method-5']);
    Route::get('/route-6', [SomeController::class, 'method-6']);
    Route::get('/route-7', [SomeController::class, 'method-7']);
    //...
});
Enter fullscreen mode Exit fullscreen mode

I would follow this approach until there are no routes left in the api.php file.

Then I end up with something like this:

- app
- bootstrap
- config
- database
- public
- resources
- routes
    + v1
        * admins.php  <--- All admin routes
        * auth.php    <--- All authentication routes
    + api.php
    + channels.php
    + console.php
    + web.php
- storage
- tests
Enter fullscreen mode Exit fullscreen mode

Now that we have extracted all the routes in their respective files, it's time to declutter our api.php file.

//This is routes/api.php

/*
 * Api version 1 routes.
 * 
 * All v1 routes of the application live here.
 * 
 * 
 */

Route::prefix('v1')->group(function () {

    /*
     * Authentication routes.
     * 
     * 
     */
    Route::prefix('auth')->group(function () {
        require_once(__DIR__ . '/v1/auth.php');
    });

    /*
     * Admins routes.
     * 
     * 
     */
    Route::prefix('admins')->group(function () {
        require_once(__DIR__ . '/v1/admin.php');
    });
});
Enter fullscreen mode Exit fullscreen mode

Much better now!

It certainly is cleaner and a lot less intimidating for newer developers who join our team.

Now every time that I want to check a route, I ask myself, which group was this route in? As soon as I answer this question, I'm already done! I can look inside the respective file and find that route with ease.

Note
This little tip can be applied to all projects, but if your project has less than 100 routes, you might not want to go though all this trouble to make things cleaner. Keep things simple and it'll be fine.

Last Words
Hope you found this tip helpful. If you have any suggestions for future tips, please comment down below. I'd be more than happy to get in touch with you.


Link to cover image

Top comments (0)