Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting to previous page after login? Mojolicious

The title pretty much summarizes exactly my problem. I have my login controller, and I'd like to redirect to previous page after login. E.g., If I am viewing the Contact Us page, then I log in, I'd like to be redirected back to the Contact Us page. Just wondering the best way to achieve this.

like image 905
Franz Kafka Avatar asked Dec 19 '22 23:12

Franz Kafka


2 Answers

You could check referrer.

$controller->redirect_to($controller->req->headers->referrer);
like image 131
Chankey Pathak Avatar answered Jan 10 '23 15:01

Chankey Pathak


It is maybe not as beautiful solution as the one above, but I use a parameter to pass the url of 'previous' page (back_url). If the parameter is empty, I can setup a default page to land on.

my $self = shift;
my $back_url = $self->param('back_url');
$back_url = '/publications' if $back_url eq $self->req->url->to_abs or $back_url eq '';
# code here
$self->redirect_to($back_url);
like image 45
vikin9 Avatar answered Jan 10 '23 16:01

vikin9