Laravel Multiple Routes Files

In this example will make two api routes files for Admin and User:

Step 1: Create Route Files in routes directory

Create two route files: admin.api.php and user.api.php. These files will contain the respective API routes.

Step 2: Edit RouteServiceProvider.php

In the RouteServiceProvider class, update the mapApiRoutes() method to include the new route files. Here’s an example of how it should look:

 

  protected function mapApiRoutes()
    {
        Route::prefix(‘api’)
            ->middleware(‘api’)
            ->namespace($this->namespace)
            ->group(base_path(‘routes/api.php’)); // keeping this if you need to have general api routes
        Route::prefix(‘api’)
            ->middleware(‘api’)
            ->namespace($this->namespace)
            ->group(base_path(‘routes/admin.api.php’));
        Route::prefix(‘api’)
            ->middleware(‘api’)
            ->namespace($this->namespace)
            ->group(base_path(‘routes/user.api.php’));
    }
 

 

 

Step 3: Remember to clear the application cache after making these changes by running the following command:

 

php artisan cache:clear
 

With these modifications, Laravel will load the admin.api.php and user.api.php route files, allowing you to define separate API routes in each file based on their intended functionality.

 

 

Leave a Comment

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