How do I share variables between route handlers ?
In Dancer.pm there is a forward example. ------------------------------------------------------------------------------------ get qr{ /demo/articles/(.+) }x => sub { my ($article_id) = splat; # you'll have to implement this next sub yourself :) change_the_main_database_to_demo(); forward '/articles/$article_id'; }; ------------------------------------------------------------------------------------ I want to the same thing. In order to that, I set the variable in first route and pass it to forwarded route. I thought I could be used to dancer's vars variables. I tried but it did not work. Set var in before filter and read it in route handler is work fine. Set var in route handler and read it in passed route handler is work fine. Set var in route handler and read it in before_template is work fine. But Set var in route handler and read it in forwarded route handler did not work. Set var in route handler and read it in before filter did not work. Perl's my and our variables outer a route handler does not used because other user session read it. I want to variables which shared in route handlers. Or I want to the filter which is called only once in every user's access. Before filter is called every time which used forward method. Waht better way to share variables in route handlers ? -- Takeshi OKURA
On Fri, Apr 15, 2011 at 06:26:23PM +0900, Takeshi OKURA wrote:
In Dancer.pm there is a forward example. ------------------------------------------------------------------------------------ get qr{ /demo/articles/(.+) }x => sub { my ($article_id) = splat;
# you'll have to implement this next sub yourself :) change_the_main_database_to_demo();
forward '/articles/$article_id'; }; ------------------------------------------------------------------------------------
I want to the same thing. In order to that, I set the variable in first route and pass it to forwarded route.
I thought I could be used to dancer's vars variables. I tried but it did not work.
Set var in before filter and read it in route handler is work fine. Set var in route handler and read it in passed route handler is work fine. Set var in route handler and read it in before_template is work fine. But Set var in route handler and read it in forwarded route handler did not work. Set var in route handler and read it in before filter did not work.
Perl's my and our variables outer a route handler does not used because other user session read it.
I want to variables which shared in route handlers. Or I want to the filter which is called only once in every user's access. Before filter is called every time which used forward method.
Waht better way to share variables in route handlers ?
I wonder if I may have encountered a related problem. In my first attempt at a website, I had difficulty with this code from Dancer::Introduction before sub { if (!session('user') && request->path_info !~ m{^/login}) { # Pass the original path requested along to the handler: var requested_path => request->path_info; request->path_info('/login'); } }; I wasn't able to pass the original requested path. I never fully understood the problem, but eventually found a workaround: before sub { if (!session('user') and request->path_info !~ m{(login|bye)$}) { # Pass the original path requested along to the handler: session requested_path => request->path_info; redirect('/login'); } }; The path issues were complicated by my use of Apache mod_rewrite. Would be nice to go back and test this more carefully. Regards, Joel
-- Takeshi OKURA
-- Joel Roth
2011/4/15 Joel Roth <joelz@pobox.com>:
before sub { if (!session('user') && request->path_info !~ m{^/login}) { # Pass the original path requested along to the handler: var requested_path => request->path_info; request->path_info('/login'); } };
I think above example is assumed as follows. get '/login' => sub { my $requested_path = vars->{requested_path}; return <<__EOM__; <form action="/login" method="POST"> <p> <input type="hidden" name="requested_path" value="$requested_path"> Login: <input type="text" name="user" value=""> <br> <input type="submit" name="action" value="Login"> </p> </form> __EOM__ }; post '/login' => sub { session user => params->{user}; return redirect params->{requested_path}; }; Regards. -- Takeshi OKURA
On Sat, Apr 16, 2011 at 06:57:53PM +0900, Takeshi OKURA wrote:
2011/4/15 Joel Roth <joelz@pobox.com>:
before sub { if (!session('user') && request->path_info !~ m{^/login}) { # Pass the original path requested along to the handler: var requested_path => request->path_info; request->path_info('/login'); } };
I think above example is assumed as follows.
get '/login' => sub { my $requested_path = vars->{requested_path}; return <<__EOM__; <form action="/login" method="POST"> <p> <input type="hidden" name="requested_path" value="$requested_path"> Login: <input type="text" name="user" value=""> <br> <input type="submit" name="action" value="Login"> </p> </form> __EOM__ };
post '/login' => sub { session user => params->{user}; return redirect params->{requested_path}; };
Thanks, I'll try that.
Regards. -- Takeshi OKURA
-- Joel Roth
2011/4/15 Takeshi OKURA <okura3@gmail.com>:
Waht better way to share variables in route handlers ?
ambs said in thread "Bug in `forward`? " 2011/4/16 ambs <ambs+dancer@perl-hackers.net>:
get '/ticket/:tid/assign' => sub { my $tid = params->{tid}; params->{assign} = 1; return forward "/ticket/$tid/set"; };
I noticed that params can be used to share variables between route handlers. If I do not have any other good way, I want to use a one params variable for share variables. for example. params->{_stash}->{foo}, params->{_stash}->{bar} etc. Regard. -- Takeshi OKURA
participants (2)
-
Joel Roth -
Takeshi OKURA