Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SthisiskelvinK's avatar

Laravel route group not working properly

I'm using Larvel Version 6.1.0 and I want my website to support different languages. Therefore I created a new middleware and wanted to give my routes a prefix so Laravel can determine the languages.Maybe there's a better way but here's what I did so far.

The urls are supposed to look like this in the end:

mywebsite.com/en/home , url/locale/home

The middleware to set the locale

namespace App\Http\Middleware;

use Closure;

class SetLocale
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        app()->setLocale($request->segment(1));
        return $next($request);
    }
}

Registered the new middleware setlocale in kernel.php

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'setlocale' => \App\Http\Middleware\SetLocale::class,
    ];

My web.php

Route::group(['middleware' => ['setlocale'],'prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}']], function() {

Route::get('/', 'NewController@frontpage');
Route::get('/home', 'NewController@frontpage')->name("home");

Auth::routes();

});

For some reason the prefix part in my route group isn't working at all. When I enter mywebsite.com/en/home in my browser I get

Missing required parameters for [Route: login] [URI: {locale}/login]

Which is strange because I didn't request the login route but the home route and I passed a locale. Does anyone see the error or has a better idea to implement support for several languages in Laravel Version 6 ?

Thanks in advance.

1 like
7 replies
Nakov's avatar

@sthisiskelvink You should put

Auth::routes();

Out of the group, or provide a default locale instead of requiring it to exist in the URL. Because this routes are used by the framework, so you either need to modify wherever they are used and pass a locale, or just the easiest is to show a dropdown on those pages for example so that the user can chose it's own language.

You said you didn't requested the login, but you have Login on the page with a link I am guessing, so that link is missing the parameter as it is required now.

So just put those routes out of the group and handle the language chosen from those pages with a dropdown.

To make the prefix optional you can use this {locale?}.

2 likes
tisuchi's avatar
tisuchi
Best Answer
Level 70

@sthisiskelvink

You will need to take the Auth::routes() outside route group:

Route::group(['middleware' => ['setlocale'],'prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}']], function() {

    Route::get('/', 'NewController@frontpage');
    Route::get('/home', 'NewController@frontpage')->name("home");

});

Auth::routes();

The issue is being caused as the {locale} prefix is being prepended to the /login route (and all other auth routes).

6 likes
lucasweaver's avatar

Hi guys, I'm having a similar problem.

Auth::routes();

Route::group(['middleware' => 'localization', 'prefix' => '{locale?}'], function () {

   Route::get('/', 'PagesController@index')->name('welcome');

   Route::resource('courses', 'CoursesController');

   Route::resource('private-lessons', 'PrivateLessonController');

   Route::get('/pricing', 'PagesController@pricing')->name('pricing');

   Route::get('contact', 'ContactFormController@create')->name('contact');

   Route::post('/contact', 'ContactFormController@store')->name('contact');

   Route::resource('teachers', 'TeacherController');

   Route::get('/locations/{location}', 'LocationController@show');

});

Route::get('/about', 'PagesController@about')->name('about');

// Route::view('/about', 'about');

The issue for me is that any routes below the route group just redirect back to the home page. If I move the routes above the group they work, like the Auth routes.

It seems that the middleware is being applied to routes outside of the middleware group, and then every first route parameter is being used as the {locale?}, and then it's being redirected back to the home page.

When I change the middleware to include this test:

public function handle($request, Closure $next)
    {
        if (! in_array($request->locale, ['en', 'nl'])) {
            abort(400);
        }

        App::setlocale($request->locale);

        return $next($request);
    }

Then the routes throw an error instead of redirecting to the home page.

Does anyone see what I'm doing wrong here? Thanks for the help!

1 like
bugsysha's avatar

Just put those routes at the top of the route file. Problem is that the locale is optional in your case.

1 like
lucasweaver's avatar

Thanks for the reply @bugsysha . Even with making that parameter required it still caused the same error. The reason I felt that the functionality was not correct is because to my understanding of the Prefix function, it's only supposed to add the prefix to routes within the function, which therefore mean only those routes should look for that optional (or required) locale parameter.

The reason why I was hoping to get this to work as is was that my blog posts were configured to be matched just to domain.com/{slug}. I had to change the url structure of the whole blog to /blog/{slug} and also move all of the prefixed routes with locales to the bottom of the router to get it work properly now.

So it's functioning now, but I still feel like I'm missing something for optimum implementation.

1 like
bugsysha's avatar

All of your routes that are after Router::group() with optional prefix, should be moved before Route::group() with optional prefix. Route::group() with optional prefix should be the last thing you define in your routes file in that case.

1 like

Please or to participate in this conversation.