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

mvnobrega's avatar

Route ('home') for each language

When clicking on the name of the site I need the user to go to the home page for the set language.

For example: site.com/pt/posts - when the user clicks on the site icon he should be redirected to site.com/pt

site.com/br/profile - when the user clicks on the site icon he should be redirected to site.com/br

And when the user is outside the Language route group, he must be redirected to the initial site.com

Using the route name does not work, as it will call the first route with that name.

My routes look like this:

Route::group(['prefix' => 'pt', 'middleware' => 'locale:pt-br'], function () {


        Route::get('/', 'pt\perfilController@authPrefer')->name('authPreferencias');


        Route::get('/', function () {
            return view('welcome');
        })->name('home');   

});


Route::get('/', function () {
    return view('welcome');
})->name('home'); 

End my navbar:

<a href="{{ route('home') }}" class="logo"><img src="https://static.neris-assets.com/images/logo.svg" alt="16Personalities"></a>```
0 likes
2 replies
bobbybouwmann's avatar

So whenever this action trigger you just need to set the correct prefix right? Are you using some kind of packages for the languages or did you build this yourself?

The normal process for this is that you save the user language in the session. Whenever the user changes the language you update the session. Then there is a middleware that redirects you to the correct route with the correct language prefix.

mvnobrega's avatar

I solved it this way:

Route::get('/{lang?}', function ($lang = null) {

    if($lang === 'br' || $lang === null){
        return view('welcome');
    }
    abort(404);
    
})->name('home');

<a href="{{ route('home', Request::segment(1)) }}" class="logo"></a>

Please or to participate in this conversation.