Define Custom Route File with Laravel

In Laravel, you define all the application routes in routes/web.php and routes/api.php. However, sometimes you may wanna break these routes down and put them in separate files. For that we can absolutely define Custom Route File with Laravel


Worth Reading: How to Validate Form Data in Laravel


Imagine you’re building a banking system and there can be hundrads of routes. So, to organized them properly, it’s always best to seperate these routes into few files.

For example,

  • Transaction routes
  • Account Operation Routes
  • Internal System Routes
    …and so on.

In Laravel it’s very easy to add new routes file. So, let’s get started.

Let’s get started!

Firstly, create new PHP file in the routes folder to make our Custom Route File with Laravel. I’m creating a file named transactions.php.

Now, you need to register this file in the RouteServiceProvider file. So that it will load in the framework.

Route Service Provider

In the RouteServiceProvider.php create a new method called mapTransactionsRoutes(). Inside this method, you can define the middleware, namespace and group as well.

protected function mapTransactionRoutes()
{
    Route::middleware('web')
         ->namespace($this->namespace)
         ->group(base_path('routes/transaction.php'));
}

Now add that function to map() method.

public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        $this->mapTransactionRoutes();

        $this->mapSettingsRoutes();

        //add more methods
    }

So, the entire RouteServiceProvider.php file should look like this.


<?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->mapTransactionRoutes();

        $this->mapSettingsRoutes();

        //add more methods
    }

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

    /**
     * Define the "Settings" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapSettingsRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/settings.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'));
    }
}


That’s it!

Leave a Reply

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