onlinecode

Laravel 5 after login, redirect back to previous/intended url

Laravel 5 after login, redirect back to previous/intended url

In this post we will give you information about Laravel 5 after login, redirect back to previous/intended url. Hear we will give you detail about Laravel 5 after login, redirect back to previous/intended urlAnd how to use it also give you demo for it if it is necessary.

In this post, i will tell you how to redirect user back to URL they were trying to access before login using intended method.

You can also save the previous url in session so that you can easily redirect back to their previous url after authentication filter.


Example 1 : Using intended method

  1. public functionauth()
  2. {
  3. if(Auth::attempt(['email'=>$email,'password'=>$password])){
  4. // Authentication passed...
  5. returnredirect()->intended('dashboard');
  6. }
  7. }
   public function auth()
    {
        if (Auth::attempt(['email' => $email, 'password' => $password])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }


Example 2: Using Session

  1. public functiongetLogin(Request $request)
  2. {
  3. $request->session()->put('url.intended',url()->previous());
  4. returnview('login');
  5. }
  6. public functionpostLogin(Request $request)
  7. {
  8. if(Auth::attempt(['email'=>$email,'password'=>$password])){
  9. returnredirect($request->session()->get('url.intended'));
  10. }
  11. returnback();
  12. }
public function getLogin(Request $request)
{
        $request->session()->put('url.intended',url()->previous());
        return view('login');
}
public function postLogin(Request $request)
{
    if (Auth::attempt(['email' => $email, 'password' => $password])) {
        return redirect($request->session()->get('url.intended'));
    }
    return back();
}

As you see in second example, i store the previous URL in session and after successfully authenticated redirect to URL that is stored in session.

redirect() method is used to redirect user to external URL.

Label :

PHP

Laravel PHP Framework

How To

MVC

Hope this code and post will helped you for implement Laravel 5 after login, redirect back to previous/intended url. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version