[dancer-users] Again: Scope of 'hook before'
Peter Mottram
peter at sysnix.com
Thu Mar 3 15:05:36 GMT 2016
On 03/03/16 15:36, Rick Westerman wrote:
> Still looking for an answer as to the scope of a 'hook before'. Does
> it affect all of the routes or just the routes within a 'prefix'. My
> understanding is that Dancer2 does away with the globals found in
> Dancer thus I would expect that a 'hook before' within a 'prefix'
> would remain within the scope of the 'prefix' and not flow outside of
> it into the other routes. But this is not what I find thus I am
> curious as to if I am doing something wrong or have my concept wrong.
>
Scope for hooks is the current app whereas 'prefix' is more a way to
group route handlers and save some typing. In your example you load
myapp::admin into app 'myapp' so the hook is active for all routes in
app 'myapp' including get '/'.
If you want per-app hooks then you need two apps:
<code>
package myapp;
use Dancer2;
get '/' => sub {
return 'main ' . dancer_version;
};
true;
package myapp::admin;
use Dancer2;
hook 'before' => sub {
if ( dancer_app->environment ne 'rick' ) {
return halt;
}
};
get '/' => sub { return 'admin' };
true;
</code>
You also need a new app.psgi to build both apps:
<code>
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use myapp;
use myapp::admin;
use Plack::Builder;
builder {
mount '/' => myapp->to_app;
mount '/admin' => myapp::admin->to_app;
};
</code>
Notice that myapp::admin no longer uses 'prefix' since all of its routes
are relative to the mount point of the app.
R.
PeteM
> Thanks,
> -- Rick
>
> ---- Original message ----
>
> Hello. New Dancer2 user here. I am having problems with the scope of
> a 'hook before' while using 'prefix'. I am not sure if I am doing
> something dumb or if this is simply not possible or something else is
> going on.
>
> Using the prefix example from the Cookbook I have two modules
>
> package myapp;
> use Dancer2;
> use myapp::admin;
>
> prefix undef;
> get '/' => sub { return 'main ' . dancer_version };
> 1;
>
> And
>
> package myapp::admin;
> use Dancer2 appname => 'myapp';
>
> prefix '/admin';
>
> hook 'before' => sub {
> if (dancer_app->environment ne 'rick') {
> return halt;
> }
> };
>
> get '/' => sub { return 'admin' };
> 1;
>
> As expected when running with environment 'rick' then the following
> output is produced:
>
> / ==> main 0.166001
> /admin/ ==> admin
>
> But, unexpectedly to me, using the default 'development' environment
> has the app just halting as it, I presume, hits the 'hook before'.
> I would have expected '/' to return with 'main 0.166001' while
> '/admin/' to halt. In other words I would expect that the 'hook
> before' is kept within the scope of the prefix '/admin' and not bleed
> over to the undef prefix.
>
> Any ideas on what is happening?
>
> Thanks,
> --
> Rick Westerman
> westerman at purdue.edu
>
> Bioinformatics specialist at the Genomics Facility.
> Phone: (765) 494-0505 FAX: (765) 496-7255
> Department of Horticulture and Landscape Architecture
> 625 Agriculture Mall Drive
> West Lafayette, IN 47907-2010
> Physically located in room S049, WSLR building
>
>
> _______________________________________________
> 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/20160303/ff505dd3/attachment.html>
More information about the dancer-users
mailing list