TheRealPapa
TheRealPapa

Reputation: 4539

Laravel 5.4 $redirectTo not working

I am stuck with getting the redirectTo() function override in my LoginController.php as shown in the Laravel docs here.

My controller contains:

/**
 * URI where we redirect to after login
 *
 * @var string
 */
protected $redirectTo = 'player/home';

/**
 * Set route redirect
 *
 * @return mixed
 */
protected function redirectTo()
{
    dd("STOP"); <-- does not trigger

    if (session()->has('game.details')) {
        return route(session()->get('game.details.setup_route'));
    } else {
        return 'player/home';
    }
}

Why would the dd never trigger and the page always redirects to player/home? Thanks

Upvotes: 0

Views: 1053

Answers (3)

pedmindset
pedmindset

Reputation: 29

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 redirect('/home');//change the redirect here
        }

        return $next($request);
    }
}

Upvotes: 0

rhnkyr
rhnkyr

Reputation: 19

If you comment $this->middleware("guest") in the constructor of Auth\RegisterController or change the line about guest middleware in the Kernel.php it will be worked.

Upvotes: 1

TheRealPapa
TheRealPapa

Reputation: 4539

Although I did not get the method override working, I solved it by changing these lines in the login method:

    if ($this->attemptLogin($request)) {
        session()->put('game.details', Game::findByUUIDOrFail($uuid));

        $this->redirectTo = route(session()->get('game.details.setup_route'));

        return $this->sendLoginResponse($request);
    }

Upvotes: 0

Related Questions