[Dancer-users] How do I share variables between route handlers ?

Joel Roth joelz at pobox.com
Fri Apr 15 12:23:07 CEST 2011


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


More information about the Dancer-users mailing list