[dancer-users] passing a variable to a new subroutine route

Richard Reina gatorreina at gmail.com
Mon Aug 24 18:36:50 BST 2015


Hi WK,

Thanks again for the reply.  When I try it your way:

redirect "/sport_add/$id";

Dancer attempts to serve up the page /sport_add/105 (105 being an the value
of $ID) and of course gets an error. Not sure what I am doing wrong. Also,
are you saying that hook before makes the var S_id global? I have tried
invoking it briefly in other routes and it did not seem to be in scope.

Thanks for the help.

2015-08-24 11:45 GMT-05:00 WK <wanradt at gmail.com>:

> 2015-08-24 18:52 GMT+03:00 Richard Reina <gatorreina at gmail.com>:
>
> > Thank you very much for the reply. Before I saw this came up with this
> > solution to pass the variable to the other sub/template:
> >
> >   # take them to the add_sport (but with the id no)
> >     hook 'before' => sub {
> >           var S_id => $select_id;
> >           request->path('/sport_add')
> >     };
> >
> >     redirect '/sport_add';
>
> You may need before-hook sometimes, but not now (if I understood you
> correctly). before-hook is executed on every request. Mostly you don't
> have need to set id such way for every route. For basic data
> submitting is best to submit data to post-route and after successful
> data-saving redirect to result page. Like this:
>
> post '/sport_add' => sub {
>   my $id = params->{ id };
>   # DO something to store needed data
>   redirect "/sport_add/$id";
> };
>
>  get '/sport_add/:id' => sub {
>      my ( $id ) = params->{ id };
>      template 'sport_add', { id => $id };
>  };
>
> > It actually worked but I was wondering if doing it this way with hook and
> > redirect is a bad idea?
>
> The before-hook was redundant there, redirect was right way.
>
> In cookbook is shown one way to use it:
>
> http://search.cpan.org/dist/Dancer/lib/Dancer/Cookbook.pod#Before_hooks_-_processed_before_a_request
>
> > Also, I was wondering why it's a bad idea to print
> > input from dancer to the console?  Does it produce a security
> vulnerability
> > or is it merely disruptive to dancer?
>
> Have you tried to print there? You print into SТDOUT and you ruin
> http-headers, resulting faulty page. print does not send anything to
> console, use log-methods for it (like debug).
>
> Wbr,
> --
> Kõike hääd,
>
> Gunnar
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150824/4efabd6f/attachment.html>


More information about the dancer-users mailing list