re-direct to calling page

19 views
Skip to first unread message

b.du...@keele.ac.uk

unread,
Nov 22, 2017, 12:18:42 PM11/22/17
to Mojolicious
Hi there,

There's something that I don't understand about the rendering process.  I've got a method in the Controller that checks credentials from a login form, with this fragment at the end of a successful authentication

    $self->stash(user => $username);
    $self->render(template => 'tutorial/welcome', format => 'html');

when I want to redirect back to the page originally requested that required a login, I store the url in the flash with
   $self->flash(calling_page => $self->req->url);
in the method that checks if the user has logged in and then squeezed in a redirect_to call before the render

    $self->stash(user => $username);
    $self->redirect_to($self->param('calling_page')) if $self->param('calling_page');
    $self->render(template => 'tutorial/welcome', format => 'html');

Control seems to still fall through to the 'tutorial/welcome' template.  Why?  Is there an obvious reason why this doesn't work?  Or is it programmer error in how I'm saving the calling page (is putting it in a cookie better)?

many thanks,
Boyd

Dan Book

unread,
Nov 22, 2017, 12:33:38 PM11/22/17
to mojol...@googlegroups.com
"flash" puts the data into the session, not the parameters. If it's in the same request just use the stash to pass the data, if it's in a different request then you need to retrieve it from the session - https://metacpan.org/pod/Mojolicious::Controller#session . Be careful using flash since it is by nature transient, and can act strangely if the user has multiple instances of the site open, for instance.

-Dan

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscribe@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

b.du...@keele.ac.uk

unread,
Nov 22, 2017, 4:59:46 PM11/22/17
to Mojolicious
Spot on, Dan.

changing the code to
    $self->session(calling_page => $self->req->url);
and
    $self->stash(user => $username);
    $self->redirect_to($self->session('calling_page')) if $self->session('calling_page');

    $self->render(template => 'tutorial/welcome', format => 'html');
works the way I wanted.

Many thanks,
Boyd



On Wednesday, 22 November 2017 17:33:38 UTC, Dan Book wrote:
"flash" puts the data into the session, not the parameters. If it's in the same request just use the stash to pass the data, if it's in a different request then you need to retrieve it from the session - https://metacpan.org/pod/Mojolicious::Controller#session . Be careful using flash since it is by nature transient, and can act strangely if the user has multiple instances of the site open, for instance.

-Dan
On Wed, Nov 22, 2017 at 12:18 PM, <b.du...@keele.ac.uk> wrote:
Hi there,

There's something that I don't understand about the rendering process.  I've got a method in the Controller that checks credentials from a login form, with this fragment at the end of a successful authentication

    $self->stash(user => $username);
    $self->render(template => 'tutorial/welcome', format => 'html');

when I want to redirect back to the page originally requested that required a login, I store the url in the flash with
   $self->flash(calling_page => $self->req->url);
in the method that checks if the user has logged in and then squeezed in a redirect_to call before the render

    $self->stash(user => $username);
    $self->redirect_to($self->param('calling_page')) if $self->param('calling_page');
    $self->render(template => 'tutorial/welcome', format => 'html');

Control seems to still fall through to the 'tutorial/welcome' template.  Why?  Is there an obvious reason why this doesn't work?  Or is it programmer error in how I'm saving the calling page (is putting it in a cookie better)?

many thanks,
Boyd

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages