Skip to content
Advertisement

Laravel 8 redirect web routes

It seems there is an error in my Laravel 8 routes somewhere.

My web routes file:

Route::get('/{locale}', function ($locale) {
  if(!in_array($locale, ['en','nl','ru','uk','fr','de','es','pt','it']))
    abort(400);

  App::setLocale($locale);

  return view('welcome');
});

Route::get('/', function () {
  return redirect('/en');
});

Route::middleware(['auth:sanctum'])->get('/dashboard/{locale}', function ($locale) {
    if(!in_array($locale, ['en','nl','ru','uk','fr','de','es','pt','it']))
      abort(400);

    App::setLocale($locale);

    return view('dashboard');
})->name('dashboard');

Route::middleware(['auth:sanctum'])->get('/dashboard', function () {
  return redirect('/dashboard/en');
});

When going to /, it is redirected to /en. But when going to /dashboard it isn’t redirected to /dashboard/en but gives an error:

SymfonyComponentHttpKernelExceptionHttpException
http://127.0.0.1:8000/dashboard

As can be seen, not a lot of information given.

Anyone knows what to do?

Advertisement

Answer

Found it:

Laravel handles routes from top to bottom. Which means it gets to /{locale} before it gets to /dashboard. In this case dashboard is seen as {locale} and the wrong route is taken.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement