Friday, February 2, 2018

Laravel redirect to previous page after login - development

Laravel redirect to previous page after login

I use default laravel auth. In LoginController I have constuct:

public function __construct()
{
    session(['url.intended' => url()->previous()]);
    //dd(session()->get('url.intended'));
    $this->redirectTo = session()->get('url.intended');
    $this->middleware('guest')->except('logout');
}

But redirect after login not working.
Example: I have page cart, when I guest, I redirect to login from page cart. But session for some reason is main.page (/), when I do dd(session()->get('url.intended')). Why? In Cart controller I use: $user = Auth::user();.

Page login: site.com/login

Middleware:

class RedirectIfAuthenticated
{
/**
 * Handle an incoming request.
 *
 * @param  IlluminateHttpRequest  $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('/');
    }

    return $next($request);
}
}



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-redirect-to-previous-page-after-login/
via Lzo Media

No comments:

Post a Comment