Laravel Redirect Back Issues - php

I am using Laravel 8.0 to create a web application. I have multiple pages that can only be seen if you are signed into an account. I have it setup so that once you go to the page, it uses a controller function to return the view, but before that it checks if you are logged in with a simple if statement. If you are logged in, it returns the view, else it redirects to login page. I am wanting to make it so after they login it redirects them back to the page they were trying to go before. I used return redirect()->back(); to do this, but it returns them too the login page as it thinks that is where it was before. I use a separate route and controller function to login them in via the form. If anyone could help, I would much appreciate it!

You have to use following code. In your login function just add this
Session::put('previousUrl',url()->previous());
$previous_url = Session::get('previousUrl');
And after login authentication successful just call this
return Session::get('previousUrl');

i prefer use this
redirect()->route('your route name')
because in logic, after submit form, it goes to another route which is /login with POST Request
So when u use this
redirect()->back()
it goes to /login with GET Request which is your login form

Related

Laravel 4.2 Redirect::to with GET Params

Is there a possibility to redirect to an url with GET params using Redirect::to() in Laravel 4.2?
I'm trying to redirect users after login to requested page when they weren't logged in.
If you are not logged in and you visit authorized page, I save the full requested url in session (this url can be something like https://page.com/settings or https://page.com/post/23 or https://page.com/post/23?show=full. Lots of variants).
When you then log in and you have an url stored in your session, I redirect you with Redirect::to(https://page.com/post/23)
In first two cases there is no problem, but when I use Redirect::to(https://page.com/post/23?show=full) GET params are ignored.
Is there an option to redirect to url with GET params included?
There's problem with your application. If I understand you correctly, your query parameters (such as: ?param=true&param2=1) are not present in your url after redirect.
Consider the following example:
Route::get('/test', function () {
return Redirect::to('http://localhost:8000/test2?with=params');
});
Route::get('/test2', function () {
echo '1';
});
When I try to access: localhost:8000/test I get redirected to: http://localhost:8000/test2?with=params
Which is the expected behavior. Can you post an example where you do that redirect?
Also I'd suggest using: Redirect::intended() which does exactly what you want by itself.
Try this:
You can pass it like https://page.com/post/23/showFull
And you can got it via following way:
Request::segment(3);
Why not Redirect::back()? The auth filter should be able to redirect all the non-authed requests to the login page, then Redirect::back() the user to the previous visited page.
Edit: filter, not middleware.
In Laravel, By default this feature is available if you are using the auth middleware, as given below:
Route::group(array('before' => 'auth'), function(){
//authorized routes
});
Example
If the user without loging in, is accessing the url "example.com/User?abc=123", which is an authorized route.
Then the user will be redirected automatically to the login page.
After successful login the user will be automatically redirected to the url first requested ("example.com/User?abc=123")

How to remember which URL the user was accessing before registration

I am developing a Laravel application and I need some suggestion.
I have a page which when a user who is not logged in access, it redirects to the login page and after authentication, he is back to the same. This works fine.
The problem is that if the user was a new user. He doesnot have a login and goes for Registration.
He then registers and the application redirects him to several other pages (like e.g.He needs to verify his email) and when all is done he will be on the dashboard page.
Now is there any way, I can save that he was on certain page and after registering and moving through all those pages come back to the same page?
Thanks,
Santhosh
Since you haven't posted any code I'd write a general explanation of how I think you should handle this.
In short - you can get the indented url and add it to the registration button as a query parameter i.e yourdomain.com/register?origin=some_route.
So assuming you have a register button/link on your login page, add the origin to the link href:
<a href="register?origin=some_route">Register<a>
This way, when you finish the registration, you can simply access the origin by using \Input::get('origin').
Now, to actually get the intended url you can either try and get it from the Session by using:
\Session::get('url.intended', url('/'))
or you could use \Redirect::intended(url('/'))->getTargetUrl();
In both cases url('/') is used as a fallback url to the homepage and you could replace it with any other url you wish.
Hope this helps!
I imagine you could do something like this for one page:
return redirect()->intended('/dashboard');
which would the the same as
return \Redirect::intended('/dashboard');
As per docs: https://laravel.com/docs/5.2/authentication#authenticating-users
The intended method on the redirector will redirect the user to the URL they were attempting to access before being caught by the authentication filter. A fallback URI may be given to this method in case the intended destination is not available.
Alternately, if you're going through a multi-page login 'wizard' type deal, I'd personally store the initial value of redirect()->intended in a Session or Cookie, and redirect to the value of the session / cookie once your registration process is complete.
Instead of intended redirect function you can print the HTTP referer in a hidden field in the form, then after login redirect to that URL.
<input type="hidden" name="redirect" value="{{URL::previous()}}">
Controller:
if(Input::get('redirect') && (Input::get('redirect') != url('/path/to/login/'))){
return redirect(Input::get('redirect'));
}
else{
return redirect('/alternative/page');
}

Laravel 4: redirect users after login

I have page that not allowed for guests, so if guest visit it automatically redirect to login page,
the question is, Is there any simple way (included in Laravel) to return him back to first page after logged in successfully?
I know I can use with() in first redirection:
Redirect::action('UserController#login')->with('url', $_SERVER['REQUEST_URI']);
or
Redirect::action('UserController#login')->withUrl($_SERVER['REQUEST_URI']);
then store and use it in login page,
but is there something already built in Laravel,
thanks,
Yes, when your user first enters your site, Laravel stores the entry page in the Session, so, as soon as a login is successful, you can:
return Redirect::intended();
Note that this probably only work if keep using
return Redirect::guest('login');
In your filtered Route. This is the original Laravel code (filters.php) that provides this:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::guest('login');
});
You can also set a default page (just in case the intended is not available at the moment) using:
return Redirect::intended('dashboard');
And you can change the original intended by setting a new one using Session:
Session::put('url.intended', $newUrl);
As Antonio's answer states, you can use:
return Redirect::intended();
and it will redirect the user to the page the originally intended to go to, but you can pass an optional 'defualt' paramter so if they login at the login page with no intended location, it will redirect them to the default.
i.e. return Redirect::intended('dashboard');

Redirect page after login in Yii framework

I am newbie to the Yii Framework. In Yii when you login by default it redirects to the index page. I want that when I will login to Yii the page will redirect to another page not the index page. So can anyone help me in this. Any help or suggestions will be highly appreciable.
[edit]
how the redirect will work when I will use user module as after login the page is redirected towards profile page?
You can (and indeed, must, if any redirection is going to take place) specify the URL to redirect to inside your controller's actionLogin method. After a successful login, you will see something like this code:
$this->redirect(Yii::app()->user->returnUrl);
Change this to any parameter that the CController::redirect method supports, and you can control where the user is redirected after login.
As an aside, using Yii::app()->user->returnUrl enables the redirect page to return the user back to the URL they intended to visit before being redirected to the login page.
To redirect the user to a page after login, create a new controller in gii for the page your user will be directed to after s/he logs in. I'll call this controller 'app' here. Gii will automagically create some files for you- one will be /protected/models/AppController.php
In AppController.php, you will have a default public function (method) called actionIndex. The purpose of this default method is to call (render) the /protected/views/app/index.php file (also created by gii for you). index.php is the file your users will see once they log in. That is the file you will want to modify to build your app. Go back to SiteController.php and change the argument of redirect() in the actionLogin() method
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
// since my controller is /protected/controllers/AppController.php
$this->redirect(array('app/index'));
}
This should get you started. (This is essentially my post on the discussion at
the yiiframework site)
you can redirect to site/index after logged in using user module.
'modules'=>array(
// user extension
'user'=>array(
...........
# page after login
//'returnUrl' => array('/user/profile'),
'returnUrl' => array('/site/index'),
........
),
),
$this->redirect($this->createUrl('yourcontroller/youraction'));

How do I get the loginRedirect from multiple login locations in CakePHP?

I'm trying to get authentication working to my liking in a CakePHP app and running into a snag.
I want to let the user login from either the home page or from a dedicated login page. I'm using the Auth component to manage login and right now the login itself works. I am submitting the form on the home page to /Users/Login and it does log them in and create session. The problem is it then redirects the user back to the home page. I'd rather they redirect to the location specified in loginRedirect.
If i login from /users/login directly it does forward to loginRedirect. I think the problem has something to do with posting the form from one page to another page instead of to itself, auth automatically thinks you want to go back to the previous page.
Any thoughts?
in the AppController
public function beforeFilter( )
{
$this->Auth->autoRedirect = false;
}
in UsersController
public function login( )
{
if( $this->Auth->user( ) )
{
$this->redirect( array(
'controller' => 'users' ,
'action' => 'index' ,
));
}
}
Also, if you haven't already you should move the form into an element, so that you can make absolutely certain that the login form is identical between the 2 login views.
Yes auth has a feature where it will redirect you to the page you tried to access before logging in. If setting the loging redirect did not work, you can try to set the loginRedirect to false and do a manual ($this->redirect([..] ) in the UsersController::login action.
you can either turn off $autoRedirect by setting it to false and handling the redirect by yourself. The problem with the AuthComponent is, that there is too much automagic which you can't really control, or only by hacks.
One solution for your problem is deleting the Session.Auth.redirect key, so the AuthComponent will always use the $loginRedirect URL:
$this->Session->del('Auth.redirect');

Categories