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

marcgaumont's avatar

localeSessionRedirect (mcamara)

Hi guys, I have successfully integrated the "mcamara/laravel-localization" package to my app for the most part.

Routes translate fine, ex.: app.dev/login --- becomes ---> app.dev/fr/connexion The problem is there seems to be no session logged when the user authenticates... If I have logged in from the french version

I am redirected to "/" instead of "/fr/"

The localeSessionRedirect middleware does not seem to work. It is in my kernel.php as stated in the docs.

Question : Am I supposed to see a session var or cookie, if I check my inspector --> ressources, under chrome ? If so its not working.

Here is part of my routes file :

/* Localized routes*/

Route::group(['prefix' => LaravelLocalization::setLocale(), 
              'middleware' => [ 'localeSessionRedirect', 'localize', 'web' ]], function() {
    // Home           
    Route::get(LaravelLocalization::transRoute('routes.home'),array(
        'as' => 'home',
        'uses' => 'HomeController@home'
    ));
    // Food       
    Route::get(LaravelLocalization::transRoute('routes.food'),array(
        'as' => 'food',
        'uses' => 'FoodController@food'
    ));
});


/* Laravel Auth + Socialite + mcamara localization */

Route::group(['prefix' => LaravelLocalization::setLocale(), 
              'middleware' => [ 'localeSessionRedirect', 'localize', 'web' ]], function() {
    
    // Authentication Routes
    // Login Get
    Route::get(LaravelLocalization::transRoute('routes.login'),  
               array('as' => 'showLoginForm', 'uses' => 'Auth\AuthController@showLoginForm') );
    // Login Post
    Route::post(LaravelLocalization::transRoute('routes.login'), 
                array('as' => 'login', 'uses' => 'Auth\AuthController@login') );
    // Logout Get
    Route::get(LaravelLocalization::transRoute('routes.logout'), 
               array('as' => 'logout', 'uses' =>  'Auth\AuthController@logout') );

    // Registration Routes...
    // Register Get
    Route::get(LaravelLocalization::transRoute('routes.register'),  
               array('as' => 'showRegistrationForm', 'uses' => 'Auth\AuthController@showRegistrationForm') );
    // Register Post
    Route::post(LaravelLocalization::transRoute('routes.register'), 
                 array('as' => 'register', 'uses' => 'Auth\AuthController@register') );

Here is my kernel.php

        //Custom
        'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
        'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class

My 'hideDefaultLocaleInURL' => false, and 'useAcceptLanguageHeader' => true.

Any kind of hint might help.. I read somewhere moving the posts from the localized routes for the login might help but did not, but Im still open to try again if thats the reason this is not persistent :) thx guys

0 likes
41 replies
marcgaumont's avatar

@ARCANEDEV I see you posted some answer in the mcamara GIT earlier today... does your package solve this problem ?

It seems pretty good !

marcgaumont's avatar

@ARCANEDEV Nice package ! But I get the exact same problem as the mcamara one!

Am I supposed to do something to my routes for the auth ? It always redirects to the wrong language and no session in saved... why ?

I was reading somewhere to remove the POSTs out of the localziation Group and it still does not work ... any ideas ?

ARCANEDEV's avatar

Hi @marcgaumont, can you make an issue in Github issues with a full details (package config file, routes, laravel version ...) ?

Note: Check if your sessions works on your server.

Snapey's avatar

Someone else just commented on another thread that the order of the middleware might be important. Try putting web first in the list.

'middleware' => ['web', 'localeSessionRedirect', 'localize']],

If it has a hand in loading the session then you might be missing the user and their previously chosen locale when the localization is run.

2 likes
ARCANEDEV's avatar

I agree with @Snapey, but the issue is when you use this and still got the same result:

Route::group(['middleware' => ['web']], function () {
    Route::group(['middleware' => ['localeSessionRedirect', 'localize']], function () {
        // Localized routes
    });
});

BTW @marcgaumont, did you followed the ARCANEDEV/Localization installation & usage guide ? There are few changes to make.

// For example: routes.php
Route::group(['middleware' => ['web']], function () {
    Route::localizedGroup(function () {
        // Home
        Route::transGet('routes.home', [
            'as'   => 'home',
            'uses' => 'HomeController@home'
        ]);

        // Food
        Route::transGet('routes.food', [
            'as'   => 'food',
            'uses' => 'FoodController@food'
        ]);


        // Authentication Routes
        // Login Get
        Route::transGet('routes.login', [
            'as'   => 'showLoginForm',
            'uses' => 'Auth\AuthController@showLoginForm'
        ]);

        // Login Post
        Route::transPost('routes.login', [
            'as'   => 'login',
            'uses' => 'Auth\AuthController@login'
        ]);

        // Logout Get
        Route::transGet('routes.logout', [
            'as'   => 'logout',
            'uses' =>  'Auth\AuthController@logout'
        ]);

        // Registration Routes...
        // Register Get
        Route::transGet('routes.register', [
            'as'   => 'showRegistrationForm',
            'uses' => 'Auth\AuthController@showRegistrationForm'
        ]);

        // Register Post
        Route::transPost('routes.register', [
            'as'   => 'register',
            'uses' => 'Auth\AuthController@register'
        ]);
    });
});
lsteamgeo's avatar

Hi,

Pour résoudre le problème de la session ("mcamara/laravel-localization" ) :

https://github.com/mcamara/laravel-localization/commit/202eef433ef6d5511818292fde5aa2ce664b8426

Bien respecter l'ordre des Middleware dans le Route::group ( @Snapey )

Tu peux aussi faire un middlewareGroups, là aussi faudra mettre les middleware dans le bon ordre.

routes.php

Route::group(['prefix' => LaravelLocalization::setLocale(),'middleware' => ['web','localization']], function () {
    // Localized routes
}]);

Kernel.php

protected $middlewareGroups = [
'localization'=> [
        \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
        \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
        \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
    ]
];
marcgaumont's avatar

@ARCANEDEV @lsteamgeo

Hi again ! thanks guys for the answers but Im still out of luck...

I can confirm the order of the middleware does not change anything for me... all translated routes work fine. Just theres is NO session stored. Period.

The only I seem to have found while digging in the mcamara package is that some have claimed to have need to exclude the POSTs from the routes and have the POSTs not translated. I have tried this as well but with no luck.

I'm using the php artisan make:auth as my main auth. Laravel 5.2.

Could there be something within middleware/authenticate.php or the new make:auth registration/login files that is causing conflict ? Hell at this point im almost willing to git the whole project to let someone take a look.

@ARCANEDEV I am using the long version for routes as follows :

My route file:

Route::group([
    'prefix'     => Localization::setLocale(),
    'middleware' => [
        'web',
        'localization-session-redirect',
        'localization-redirect'
    ],
], function() {
    // Home           
    Route::transGet('routes.home', array(
        'as' => 'home',
        'uses' => 'HomeController@home'
    ));
    // Food       
    Route::transGet('routes.food', array(
        'as' => 'food',
        'uses' => 'FoodController@food'
    ));
});

/* Laravel Auth + Socialite + mcamara localization */

Route::group([
    'prefix'     => Localization::setLocale(),
    'middleware' => [
        'web',
        'localization-session-redirect',
        'localization-redirect'
    ],
], function() {
    
    // Authentication Routes
    // Login Get
    Route::transGet('routes.login', array('as' => 'showLoginForm', 'uses' => 'Auth\AuthController@showLoginForm') );
    
    // Login Post
    Route::transPost('routes.login', array('as' => 'login', 'uses' => 'Auth\AuthController@login') );
    
    // Logout Get
    Route::transGet('routes.logout', array('as' => 'logout', 'uses' =>  'Auth\AuthController@logout') );

    // Registration Routes...
    // Register Get
    Route::transGet('routes.register', array('as' => 'showRegistrationForm', 'uses' => 'Auth\AuthController@showRegistrationForm') );
    // Register Post
    Route::transPost('routes.register', array('as' => 'register', 'uses' => 'Auth\AuthController@register') );

    // Password Reset Routes... (NOT TRANSLATED YET)
    Route::get('password/reset/{token?}', array('as' => 'showResetForm', 'uses' => 'Auth\PasswordController@showResetForm') );
    Route::post('password/email', array('as' => 'sendResetLinkEmail', 'uses' => 'Auth\PasswordController@sendResetLinkEmail') );
    Route::post('password/reset', array('as' => 'reset', 'uses' => 'Auth\PasswordController@reset') );

    // Socialite Login (NOT TRANSLATED YET)
    Route::get('/login/{provider?}',[
        'as'   => 'auth.getSocialAuth',
        'uses' => 'Auth\SocialiteAuthController@getSocialAuth'
    ]);
    Route::get('/login/callback/{provider?}',[
        'as'   => 'auth.getSocialAuthCallback',
        'uses' => 'Auth\SocialiteAuthController@getSocialAuthCallback'
    ]);
});

localization.php

<?php

return [
    /* ------------------------------------------------------------------------------------------------
     |  Settings
     | ------------------------------------------------------------------------------------------------
     */
    'supported-locales'      => ['en', 'fr'],

    'accept-language-header' => true,

    'hide-default-in-url'    => false,

    'facade'                 => 'Localization',

    /* ------------------------------------------------------------------------------------------------
     |  Route
     | ------------------------------------------------------------------------------------------------
     */
    'route'                  => [
        'middleware' => [
            'localization-session-redirect' => true,
            'localization-cookie-redirect'  => false,
            'localization-redirect'         => true,
            'localized-routes'              => true,
            'translation-redirect'          => true,
        ],
    ],

Example lang/routes file

<?php
return [
    "home"       =>  "",
    "food"       =>  "manger",
    "login"      =>  "connexion",
    "logout"      =>  "deconnexion",
    "register"      =>  "devenir-membre",
];

anything seem out of place ?

ARCANEDEV's avatar

Hi @marcgaumont,

If you're going to use the long version for the routes, you need to specify it like this:

Route::group(['middleware' => ['web']], function () {
    Route::group([
        'prefix'     => Localization::setLocale(),
        'middleware' => [
            'localization-session-redirect',
            'localization-redirect',
            'localized-routes',  // !!! ---- Required --- !!!
        ],
    ], function() {
        // Localized routes here
    });

    // Non Localized routes here
});

The Route::localizedGroup() add auto-magically the enabled middleware(s) from localization.php config file (and with a single line).

And i see that your translation file route.php contains empty values (like home), i recommend that you remove it from the translation file and do this instead:

// The home page is gonna be the '/' (aka root)
Route::get('/', array(
    'as' => 'home',
    'uses' => 'HomeController@home'
));

Note: I don't think that Route::get('', ...) gonna work with empty string !! the correct way is to use Route::get('/', ...), also the root / is for all locales and does not need to be translated.

For the socialite routes, try to add a prefix to avoid the conflicts with the basic login routes (add oauth prefix for example).

Honestly, i did not test the generated routes by the php artisan make:auth command, but i've no issue with the sessions.

Last thing to check is the translations files, when you say lang/routes file you mean that you have two routes.php files in resources/lang/en and resources/lang/fr folders ?

<?php
// resources/lang/en/routes.php
return [
    "food"     =>  "food",
    "login"    =>  "login",
    "logout"   =>  "logout",
    "register" =>  "register",
];

And

<?php
// resources/lang/fr/routes.php
return [
    "food"     =>  "manger",
    "login"    =>  "connexion",
    "logout"   =>  "deconnexion",
    "register" =>  "devenir-membre",
];

Can you also run the php artisan route:list command to see the applied middleware for the AuthController ??

What you get when you hit the / (without the locale) ?

lsteamgeo's avatar

Hello @marcgaumont ,

Est-ce que tu peux poster la vue de ton formulaire de ta page Login.

Dans la vue du formulaire de Login, tu dois spécifier la langue en cours :

<form  role="form" method="POST" action="{{ LaravelLocalization::getLocalizedURL(null,'/login') }}">

ou

{{ Form::open(array('url' => LaravelLocalization::getLocalizedURL(null,'/login'), 'method' => 'POST')) }}
freel's avatar

Hi, yes very similar issue as post author seems that package not storing locale in session.

freel's avatar

I have strange thing. Not sure it should work like that or no. but lets say I am changing my locale by browsing to ru. lets say to http://project27.dev/ru/about-us when I typing in the url http://project27.dev it redirects me to http://project27.dev/en

it seems that sessions or cookies not working... well I have my routes in the group.

Route::group([
    'prefix' => LaravelLocalization::setLocale(),
    'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ]
], function()
{
//my routes are here.

});

@lsteamgeo have you ever had this problem?

lsteamgeo's avatar

@freel,

In your Route.php can you try to put this :

Route::group(['prefix' => LaravelLocalization::setLocale(),'middleware' => ['web','localization']], function () {
    // Localized routes
}]);

and in your Kernel.php (in the same order) :

protected $middlewareGroups = [
    'localization'=> [
            \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
            \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
            \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
    ]
];
freel's avatar

@lsteamgeo thx for answer. its still the same... it seems that it not saving locale to session or after refresh not taking it from there.

can't understand whats the problem.

lsteamgeo's avatar

@freel ,

have you enable "ru" option in the config file of mcarama ? (by default there is only "en" and "es" enabled)

freel's avatar

if u talking about:

//'ce'          => ['name' => 'Chechen',                'script' => 'Cyrl', 'native' => 'нохчийн мотт', 'regional' => 'ce_RU'],
        'ru'          => ['name' => 'Russian',                'script' => 'Cyrl', 'native' => 'русский', 'regional' => 'ru_RU'],
        //'sah'         => ['name' => 'Yakut',                  'script' => 'Cyrl', 'native' => 'саха тыла', 'regional' => ''],
        

so yes.

lsteamgeo's avatar

@freel,

Ok. Have you try to get the current Locale in a view ? {{ LaravelLocalization::getCurrentLocale() }}

freel's avatar

at this moment working on this and i find out that if url don't have parameters its giving me default locale not what is in the session. in other words:

http://project27.dev/ru returns me ru

but if I am changing to http://project27.dev returns me en

freel's avatar

what you have in config/app.php

 'locale' => 'en','ru',

or

 'locale' => 'en',

or something else?

lsteamgeo's avatar

In config/laravellocalization.php , I've uncommented the line en, and fr in my case.

freel's avatar

I think its time to try everything do from the 0. Will try create new laravel install and on fresh laravel will add this package.

Next

Please or to participate in this conversation.