skip to Main Content

I have a Laravel 8.40 backend with many routes, and when I was adding scribe to the project I realized that many routes are twice listed.

Calling php artisan route:list as example, it will duplicate the route api/hq/v1/account/login, removing the v1/account portion:

POST    api/hq/login            login   AppHttpControllersHqAccount@logIn
POST    api/hq/v1/account/login login   AppHttpControllersHqAccount@logIn

I want to have just api/hq/v1/account/login.

routes/api.php

Route::group(['prefix' => 'hq'], function(){
    includeRouteFiles(__DIR__.'/api/hq/');
});

routes/api/hq/index.php

Route::group(['prefix' => 'v1'], function(){
    Route::group(['prefix' => 'account'], function(){
        includeRouteFiles(__DIR__ .  '/account/');
    });
});

routes/api/hp/account/index.php

Route::post('/login', [Account::class, 'logIn'])->name('login');

Already ran php artisan route:clear.

How can I fix it?

Thank you all in advance!

2

Answers


  1. includeRouteFiles is a helper method from laravel-boilerplate that uses RecursiveDirectoryIterator to get the list of route files to be included.

    If you call includeRouteFiles in a route file that is already being included by the same method, you’ll load its routes twice.

    Login or Signup to reply.
  2. You can try change this.

    //1st
    Route::group(['prefix' => 'hq'], function(){
        includeRouteFiles(__DIR__.'/api/hq/');
    });
    
    //2nd
    Route::group(['prefix' => 'v1'], function(){
       Route::group(['prefix' => 'account'], function(){
          includeRouteFiles(__DIR__ .  '/account/');
     });
    });
    

    Into

    //1st
    Route::prefix('hq')->group(__DIR__.'/web/api/hq/index.php');
    //2nd
    Route::group(['prefix' => 'v1'], function(){
       Route::prefix('account')->group(__DIR__.'/web/api/hq/account/index.php');
    });
    

    Note

     includeRouteFiles(__DIR__ .  '/api/hq/');
    //This will include all files in directories and subdirectories of /api/hq/ recursively.
    

    Answer is based on Organizing your routes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search