Laravel 5 - After login redirect back to previous page

前端 未结 13 2332
广开言路
广开言路 2020-11-30 04:41

I have a page with a some content on it and a comments section. Comments can only be left by users who are signed in so I have added a login form to the page for users to si

相关标签:
13条回答
  • 2020-11-30 05:03

    Please use redirect()->intended() instead in Laravel 5.1

    You can also see more about it here: http://laravel.com/docs/5.1/authentication

    0 讨论(0)
  • 2020-11-30 05:03

    add this to LoginController:

    protected function redirectTo(){
        return url()->previous();
    }
    

    Note: if present the field $redirectTo , remove it

    0 讨论(0)
  • 2020-11-30 05:05

    You can use redirect back with Laravel 5:

    <?php namespace App\Http\Controllers;
    
    use Redirect;   
    class SomeController extends Controller { 
        public function some_method() {
           return Redirect::back()
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:07

    Solution for laravel 5.3:

    In loginController overwrite the showLoginForm() function as this one:

    public function showLoginForm()
    {
        if(!session()->has('url.intended'))
        {
            session(['url.intended' => url()->previous()]);
        }
        return view('auth.login');    
    }
    

    It will set the "url.intended" session variable, that is the one that laravel uses to look for the page which you want to be redirected after the login, with the previous url.

    It also checks if the variable has been set, in order to avoid the variable to be set with the login url if the user submit the form with an error.

    0 讨论(0)
  • 2020-11-30 05:10

    in your RedirectIfAuthenticated.php change this code

    public function handle($request, Closure $next, $guard = null)
        {
            if (Auth::guard($guard)->check()) {
                return redirect()->intended('/contactus');
            }
    
            return $next($request);
        }
    

    please notice to :

    return redirect()->intended('/contactus');
    
    0 讨论(0)
  • 2020-11-30 05:12

    For Laravel 5.5, following code worked for me by just updating LoginController.php

    public function showLoginForm()
    {
        session(['link' => url()->previous()]);
        return view('auth.login');
    }
    
    
    protected function authenticated(Request $request, $user)
    {
        return redirect(session('link'));
    }
    
    0 讨论(0)
提交回复
热议问题