akrug23's avatar

Accessing a named route when your route file has been split into multiple files

I have my routes in multiple files and I am including them in the web.php file with the code below.

// routes/web.php

Route::name('resources')
  ->prefix('resources')
  ->group(base_path('routes/web/resources.php'));

One of my files contains all my redirects. I am trying to access the routes by their name to use on the redirect but I am getting an error saying the route name doesn't exist. Is this possible?

resources route file

// routes/web/resources.php

Route::get(
  '/about',
  [PageController::class, 'showAboutPage']
)->name('about');

redirect route file

// routes/web/redirects.php

Route::get('/test', function () {
  return redirect(null, '301')->route('about');
});
0 likes
2 replies
Niush's avatar
Niush
Best Answer
Level 50

Try doing Route::name('resources.') (suffix with dot). Then, you can use the route name prefixed with that group name. Example: route('resources.about')

Route::name('resources.') // <- Prefixes the route name e.g. resources.about
  ->prefix('resources') // <- Prefixes the route path URL e.g. /resources/about
  ->group(base_path('routes/web/resources.php'));

You can also run php artisan route:list command to see the list of routes, their controller and name.

1 like

Please or to participate in this conversation.