简体   繁体   中英

Laravel 5.5 RedirectTo not working in LoginController

I need your help.

When a user login I need to direct them one multi language router and from what I've read I simply add RedirectTo in my LoginController, but doing so has no effect:

class LoginController extends Controller
{
.
.
     //protected $redirectTo ='/Utilisateur';(it's working)
     protected $redirectTo =  route('displayNew');(it isn't working)
.
.
}

INFO : In fact, on Laravel after the login, the user is redirected to home (redirection by default), to change this behavior, we must give a value to the redirectTo variable. The function that treats this variable is:

trait RedirectsUsers{ 
public function redirectPath(){
   if (method_exists($this, 'redirectTo')) {
        return $this->redirectTo();
    }

    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
}

Class properties has to be a "static" value on creation, ie a set value. You cannot use another PHP function to assign a value in the declaration, . You can get around it by setting the value in the constructor:

class LoginController extends Controller
{
     protected $redirectTo = '';

     public function __construct() {
         $this->redirectTo = route('displayNew');
     }

}

If you need more robust customization of the response returned when a user is authenticated, Laravel provides an empty authenticated(Request $request, $user) method that may be overwritten if desired:

put this method in LoginController.php

protected function authenticated(Request $request, $user)
{
        return redirect()->route('displayNew');
}

If you have run php artisan auth

change the RedirectIfAuthenticated Middleware like so

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {

            return route('displayNew');//change the redirect here
        }

        return $next($request);
    }
}

And in your route file you can do like so

Route::get('displayNew', 'MultiLanguageController@index')->name('displayNew');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM