I have to create ,,log in to comment" button. On click this button should redirect user to login page and after successfull loging in, he should be redirected to page, where he have clicked that button. I tried to make fake url with auth middleware around it like this:
Route::group(['middleware' => 'auth'], function() {
Route::get('/log-in-to-comment', 'UserController@getLogInToComment');
});
I have defined my getLogInToComment
function like this:
public function getLogInToComment() {
return redirect()->back();
}
But it redirects me to /log-in-to-comment
which is already in filter so I get too many redirects. Also I have tried return Redirect::to(URL::previous());
but it also doesn't work. As far as I know, post method should not be used on redirects, so how do I deal with this simple task? Thanks in advance!
Since Laravel always redirects back to the page you came from (which is in your case /log-in-to-comment), you have to overwrite this URL.
A possible way to do so is to fetch the URL of the last site and an save it to the browsers session. To do so, make sure the getLogInToComment-route is not using auth or guest middleware, since it should be processed as a guest as well as a signed in user. Then, store the previous site if the user is not logged in, and redirect back to it if he is.
public function getLogInToComment() {
if (!Auth::user()) {
// User is not logged in. Save previous URL and redirect to login
Session::put('prev', URL::previous());
return Redirect::route('login');
} else {
// User is now logged in and returned to this route again. Redirect to site
return Redirect::to(Session::get('prev'));
}
}