0

I can see this in RouteServiceProvider.php:

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

But when I try to add one:

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

Then it doesn't pick up the second web.php file. But if I remove the first web.php reference, then the second one is picked up. So it seems Laravel only wants one web.php. However, even this doesn't work:

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

Is there any way to get a second web.php file to work?

I've tried/considered:

  • A workaround by including the web.php file from the main web.php file. But I'd prefer it if the route provider handled it.

Any ideas?

UPDATE 1

I tried this but it doesn't seem to work for Laravel 8:

protected function mapApiRoutes()
{
    Route::prefix('api')
         ->middleware('api')
         ->namespace($this->namespace)
         ->group(function ($router) {
            require base_path('routes/admin.php');
            require base_path('routes/category.php');
        });
}
rockstardev
  • 13,479
  • 39
  • 164
  • 296
  • I made a subfolder in my routes folder called test and a web.php file in it, for me this is working `routes/test/web.php`. Are you sure your file is located at `themes/myTheme/web.php`. I also made a subfolder called test at `test/web.php` (so not in the routes folder) this is also working for me... – Aless55 Jul 21 '21 at 06:43
  • Yes, I know it's at the right place because it works if I remove the previous one. But the other route is not in /routes. It's actually in /storage/sites/mysite/routes/web.php. Would be odd if it has anything to do with it. – rockstardev Jul 21 '21 at 06:45
  • Does this answer your question? [multiple route file instead of one main route file in laravel 5](https://stackoverflow.com/questions/34182806/multiple-route-file-instead-of-one-main-route-file-in-laravel-5) – OMi Shah Jul 21 '21 at 06:46
  • @OMiShah I think I might just go that route. Scuse the pun. – rockstardev Jul 21 '21 at 06:47
  • @coderama Hmm it probably has something to do with it, I mean I can only access items in my storage via requests. So your file might be hidden. – Aless55 Jul 21 '21 at 06:48

3 Answers3

2

In your RouteServiceprovider.php file modify the below part:

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

and make it as:

Route::middleware('web')
      ->namespace($this->namespace)
      ->group(function ($router) {
            require base_path('routes/web.php');
            require base_path('routes/test.php'); // replace the route file path
      });

and then make sure to clear the cached routes using the command:

php artisan route:clear

OMi Shah
  • 5,768
  • 3
  • 25
  • 34
1
  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'));

  Route::middleware('web')
      ->namespace($this->namespace)
      ->group(base_path('storage/routes/web2.php')); // 'themes/myTheme/web.php' in your case

This will work, but don't forget to run php artisan route:cache command after the change

Nidecker
  • 172
  • 8
  • Nope. Doesn't want to work. I'll fiddle a bit more. I'm obviously doing something wrong if it's working for you. – rockstardev Jul 21 '21 at 07:01
0

Another way, so you don't have to keep updating your provider whenever you add a new file is to move your routes to a folder and loop inside it.

   Route::prefix('api')
         ->middleware('api')
         ->namespace($this->namespace)
          ->group(function () {
                foreach (glob(base_path('routes/api/*.php')) as $fileName) {
                    require $fileName;
                }
            });
  
Wushu06
  • 93
  • 2
  • 10