How to Create a Custom Route File in Laravel

In this article, we will discuss “How to Create a Custom Route File in Laravel”. You can found all the route files in the “routes” directory. Laravel provides an easy way to manage all our application route from some of the specific files such as the “routes/web.php” and the “routes/api.php” are the files handle routes for the web and API respectively.

Sometimes we need to create an additional route file, maybe when you want to separate routes for each version of your API. Or in any large project where you want to differentiate your routes.

Create a Custom Route File

Let’s create a new route file titled “api-v2.php” in the routes directory. You need to register the new route file in the “RouteServiceProvider”. After updating the service provider, all our routes defined in this file will be loaded automatically.

In the “RouteServiceProvider” class, we need to create a method called “mapApiV2Routes”. In this method, we need to define the middleware, namespace of the controllers that will handle requests for the routes, the route prefix, alias and the path to the route file.

/**
 * Custom routes for API V2
 */
protected function mapApiV2Routes()
{
    Route::prefix('api/v2')
        ->middleware('api')
        ->namespace($this->namespace)
        ->group(base_path('routes/api-v2.php'));
}

It’s time to register the “mapApiV2Routes” method in the “map” method.

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        $this->mapApiV2Routes();
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }


    /**
     * Custom routes for API V2
     */
    protected function mapApiV2Routes()
    {
        Route::prefix('api/v2')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api-v2.php'));
    }
}

Conclusion

In this article, we are discussing how to create a custom route file in Laravel. Hope you like this article. You can check the official documentation here for routing. Please feel free to add the comment for any query or submit your feedback 😉


You may like:

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

LaravelLaravel 5.7Laravel5Laravel5.6Route
Comments (2)
Add Comment
  • James

    hi iwant to know the code inside api-v2.php

  • Nuttapong Sarkana

    thank you.