1

I'm using route::auth() for validate user login/logout. But when I write the localization by this post https://laracasts.com/discuss/channels/tips/example-on-how-to-use-multiple-locales-in-your-laravel-5-website

I couldn't have a right way, it redirect to '/' when I logout by using $redirectPath in AuthController. I can config it to '/en' but it hard code. I want it redirect to current locale(which before login/logout). Example: Current path is: localhost:8888/fr/user, then after logout is localhost:8888/fr/login, as you see, it automatic, not hard code as I can config.

Thanks,

Henry Bui
  • 423
  • 3
  • 17

6 Answers6

1

You can do a workaround. Set the $redirectPath to just 'login' and from there redirect to the current locale:

In your controller:

$redirectPath = '/login';

In your routes.php:

Route::get('/login', function () {
    return redirect('/' . App::getLocale(). '/login');
});
thefallen
  • 9,496
  • 2
  • 34
  • 49
0

I've been searching for this issue for a while now and finally found the answers.

Most of the articles describing how to configure a Middleware for setting the app locale, fail to mention an important part. That is, addressing the redirect when login, logout, register, reset password and verify email routes. These all will fallback to the fallback_locale.

Many found their way of working around this issue, by saving the locale in the session and access this variable in different Middlerwares, Controllers, etc... Not too bad of a workaround, however, I believe for an intuitive framework like Laravel there must be a better way, so I kept searching...

Here is the answer:

You might want to add a redirect rule to each of them, so when that route is called, it will apply the locale you have set for the rest of the application. (Multi-Language Routes and Locales with Auth)

The solution would be the add a redirect method to them:

  • App\Http\Controllers\Auth\LoginController.php
  • App\Http\Controllers\Auth\RegisterController.php
  • App\Http\Controllers\Auth\ResetPasswordController.php
  • App\Http\Controllers\Auth\VerificationController.php

Add the redirect method to each of those:

public function redirectTo()
{
    return app()->getLocale() . '/home';
}

Almost all works except one, the logout. This route is also handled by the LoginController, however, our redirect rule doesn't apply for it. You might need to override the logout() method. (How to set laravel 5.3 logout redirect path?)

App\Http\Controller\Auth\LoginController.php

// use AuthenticatesUsers;
use AuthenticatesUsers {
    logout as performLogout;
}

protected $redirectTo = '/home';

public function __construct()
{
    $this->middleware('guest')->except('logout');
}

public function redirectTo()
{
    return app()->getLocale() . '/home'; // works for login
}

public function logout(Request $request)
{
    $this->performLogout($request); // call the original code
    return redirect(app()->getLocale() . '/home'); // add the Locale fix for logout
}

I am using Laravel 6, but the solutions were discussed for earlier versions

Attila Antal
  • 811
  • 8
  • 17
0

I use this package for Localization - https://github.com/mcamara/laravel-localization . put Route::auth(); in prefix localization

Route::group(['prefix' => LaravelLocalization::setLocale() , 'middleware' => 
['localeSessionRedirect', 'localizationRedirect', 'localeViewPath']], function () {
  Route::auth();
});

it will work .

Mohamed Raafat
  • 122
  • 1
  • 5
0

Updating for Laravel 8: This localization problem is still not complained anywhere, so here's a solution for fixing login,logout,and registration in locale:

return redirect()->intended(route('home',app()->getLocale()));

same for the others!

Kiyomi
  • 1
0

Here is another approach for Laravel 9 with Fortify 1.17

the native script of Fortify .\vendor\laravel\fortify\src\Http\Responses\LogoutResponse.php redirect to a static URL string / instead of using the dynamic route() function. So, you can change that behavior like so :

// .\app\Providers\FortifyServiceProvider.php > register()
use Laravel\Fortify\Contracts\LogoutResponse;
...
$this->app->instance(LogoutResponse::class, new class implements LogoutResponse
{
    public function toResponse($request)
    {
        $redirectResponse = app(\Laravel\Fortify\Http\Responses\LogoutResponse::class)->toResponse($request);

        if (get_class($redirectResponse) == \Illuminate\Http\RedirectResponse::class)
        {
            return redirect(route('home')); // add here your locales parameters
        }

        return $redirectResponse;
    }
});
Lenor
  • 1,471
  • 10
  • 19
-1

Assuming your current locale is set in config/app.php.

You can get your locale from config using config('app.locale');.

And you can then use that value in redirect() or set $redirectPath value as that in runtime.

z3r0ck
  • 683
  • 1
  • 6
  • 15