Search code examples
laravelauthenticationlocalizationmultilingual

Laravel - Redirect users to custom urls based on language field on login form


I have a Laravel project with the basic (artisan) auth created. I have added a language field to the login.blade.php view.

I would like to catch that language variable from the login form and redirect users after login to myawesomewebsite/{$language}

(myawesomewebsite/en or myawesomewebsite/fr).

I have no idea how to catch this data.

I think it would be nice if I am able to store this language variable in the sessions and then I could use this code to redirect the user to my routes. I'm using this code in my LoginController.php so far.

public function authenticated(Request $request)
{
    $lang = $request->session()->get('user_language', 'default');
    $route = '/' . $lang;
    return redirect($route);
}

, but if you know some other way better, please share it. Thanks


Solution

  • If you have a language input field in the form, you can just use:

    $request->input('user_language', 'default');

    Other than that, your code should work as long as you have a route supporting the language.