Laravel Redirect to Previous after login

I have a login form and after submitting the form (if validation is correct), the browser should redirect to the last page before entering . I am currently being redirected back and I always get to the same login page. My route.php looks something like this:

Route::get('/', ' HomeController@index '); Route::get('/list',' EventController@index '); Route::get('/login',' AuthController@login '); Route::post('/login',' AuthController@do _login'); 

And my redirection inside do_login () is

 if(Login_is_valid()) { return Redirect::back(); } 

If I am inside the / list and then open the Login and fill out the form correctly, I am redirected to / login again, isn't that strange? Many thanks

+5
source share
2 answers

Hello, if you are using Laravel 5.1, you can redirect to the previous page as follows

 return back(); 

or you can use Session where Laravel saves the previous page

 return redirect(Session::get('_previous')['url']); 
+8
source

Auth / LoginController has protected $redirectTo = '/home'; change '/ home' to '/ loginin' for example, and create a new route similar to this

 Route::get('/loginin', function () { $page = '/' . session()->get('page'); if($page != '') { session()->put('page', ''); return redirect($page); } else { return redirect('/home'); } }); 

or you can put this in the controller and call it from the route

and in every function in the page controller this code

 session->put('page','/name_of_the_page'); 
0
source

All Articles