In this tutorial, I will show you how to define routes outside the web.php file in Laravel 9. Assume you have a big application and you are going to develop it using the PHP Laravel framework. You know that we write all of the routes in web.php.

So from this example, you will learn Laravel 9 create custom route file. I will also show you a way how to create multiple custom routes in Laravel.

What if we can write routes in our custom file as many as you want? Yes, if we can do it then our every route file will be easily maintainable. So, from this tutorial, you will learn how to write Laravel 8 multiple route files in the Laravel 9 application.

laravel-9-multiple-routes-file-example

Let's assume we have more than one route file whose name is report.php. So if you make it useable then first visit the following path and add the below line below:

App\Providers\RouteServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/dashboard';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    // protected $namespace = 'App\\Http\\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));

            // add this new line
            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/report.php'));

        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}

 

Now report.php is our custom route file and it can be used in your entire application.

routes/report.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::get('your_another_url', function (Request $request) {
    return $request;
});

 

Now let's solve it using recursively if there are multiple route files. Assume we have multiple custom route files in the Laravel 9 application. Now we can solve it by the following statement by adding it inside the boot() method in our routeServiceProvider.php.

App\Providers\RouteServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/dashboard';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    // protected $namespace = 'App\\Http\\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
        
        /**
         * add this code
         */
        Route::group([
                'middleware' => 'web',
                'namespace' => $this->namespace,
            ], function ($router) {
                $routes = glob(base_path('routes/*.php'));
                foreach ($routes as $route) {
                    require $route;
            }
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}

 

Now all are set to go. Create as many routes as you want in your Laravel applications.

 

Read also: Laravel 9 Resource Controller And Route Example

 

Hope it can help you.

Category : #laravel

Tags : #laravel , #laravel routing