<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">On 03/03/16 15:36, Rick Westerman
wrote:<br>
</div>
<blockquote cite="mid:56D84BF0.1010202@purdue.edu" type="cite">
<meta http-equiv="content-type" content="text/html;
charset=windows-1252">
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.<br>
<br>
</blockquote>
<br>
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 '/'.<br>
<br>
If you want per-app hooks then you need two apps:<br>
<br>
<code><br>
<br>
<font face="Courier New, Courier, monospace">package myapp;<br>
use Dancer2;<br>
<br>
get '/' => sub {<br>
return 'main ' . dancer_version;<br>
};<br>
<br>
true;<br>
<br>
package myapp::admin;<br>
use Dancer2;<br>
<br>
hook 'before' => sub {<br>
if ( dancer_app->environment ne 'rick' ) {<br>
return halt;<br>
}<br>
};<br>
<br>
get '/' => sub { return 'admin' };<br>
<br>
true;</font><br>
<br>
</code><br>
<br>
You also need a new app.psgi to build both apps:<br>
<br>
<code><br>
<font face="Courier New, Courier, monospace"><br>
#!/usr/bin/env perl<br>
<br>
use strict;<br>
use warnings;<br>
use FindBin;<br>
use lib "$FindBin::Bin/../lib";<br>
<br>
use myapp;<br>
use myapp::admin;<br>
use Plack::Builder;<br>
<br>
builder {<br>
mount '/' => myapp->to_app;<br>
mount '/admin' => myapp::admin->to_app;<br>
};</font><br>
<br>
</code><br>
<br>
Notice that myapp::admin no longer uses 'prefix' since all of its
routes are relative to the mount point of the app.<br>
<br>
R.<br>
PeteM<br>
<br>
<blockquote cite="mid:56D84BF0.1010202@purdue.edu" type="cite">
Thanks,<br>
-- Rick<br>
<br>
---- Original message ---- <br>
<br>
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.<br>
<br>
Using the prefix example from the Cookbook I have two modules<br>
<br>
<font face="Courier New, Courier, monospace">package myapp;<br>
use Dancer2;<br>
use myapp::admin;<br>
<br>
prefix undef;<br>
get '/' => sub { return 'main ' . dancer_version };<br>
1;</font><br>
<br>
And<br>
<br>
<font face="Courier New, Courier, monospace">package myapp::admin;<br>
use Dancer2 appname => 'myapp';<br>
<br>
prefix '/admin';<br>
<br>
hook 'before' => sub {<br>
if (dancer_app->environment ne 'rick') {<br>
return halt;<br>
}<br>
};<br>
<br>
get '/' => sub { return 'admin' };<br>
1;</font><br>
<br>
As expected when running with environment 'rick' then the
following output is produced:<br>
<br>
<font face="Courier New, Courier, monospace">/ ==> main
0.166001<br>
/admin/ ==> admin<br>
</font><br>
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.<br>
<br>
Any ideas on what is happening? <br>
<br>
Thanks,<br>
<pre class="moz-signature" cols="72">--
Rick Westerman
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:westerman@purdue.edu">westerman@purdue.edu</a>
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</pre>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
dancer-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:dancer-users@dancer.pm">dancer-users@dancer.pm</a>
<a class="moz-txt-link-freetext" href="http://lists.preshweb.co.uk/mailman/listinfo/dancer-users">http://lists.preshweb.co.uk/mailman/listinfo/dancer-users</a>
</pre>
</blockquote>
<br>
</body>
</html>