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

WK wanradt at gmail.com
Mon Aug 24 17:45:30 BST 2015


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


More information about the dancer-users mailing list