4

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   App\Http\Controllers\Hq\Account@logIn
POST    api/hq/v1/account/login login   App\Http\Controllers\Hq\Account@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!

Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81
  • 1
    did you try to clear route cache? `php artisan route:clear` – Mr. Kenneth Aug 10 '23 at 00:17
  • 1
    you could also try to delete `cache/route.php` based on this answer: https://stackoverflow.com/a/37879020/13933721 – Mr. Kenneth Aug 10 '23 at 00:18
  • Yes, I already cleared it many times, and it is still happening. – Tiago Gouvêa Aug 10 '23 at 15:00
  • what do those `includeRouteFiles(...)` contains? that self made thingy might be able to point to the same controller twice, but [who knows](https://stackoverflow.com/q/51797366). especially the routes they append into the list. – Bagus Tesa Aug 15 '23 at 12:18
  • 2
    The include in `api.php` is directly including both files `routes/api/hq/index.php` **and** `routes/api/hp/account/index.php`. So it is normal that you get the route `'/api/hq/'` + `'/login'` when the second one is included. It all comes back to the method `includeRouteFiles` which includes also subdirectory files. – N69S Aug 16 '23 at 18:46
  • try `php artisan optimize` – Saddam Aug 17 '23 at 17:09

2 Answers2

4

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.

1

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');
});

Now you should have api/hq/v1/account/login.

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

Ali Raza
  • 155
  • 7