[dancer-users] Route dispatch

Celogeek me at celogeek.com
Fri Dec 28 19:40:03 GMT 2012


It seems that the new way to do is to create a Dancer::Plugin that add a hooks. Then use it ! 


----- Mail original -----

De: "Celogeek" <me at celogeek.com> 
À: "Perl Dancer users mailing list" <dancer-users at dancer.pm> 
Envoyé: Vendredi 28 Décembre 2012 20:33:02 
Objet: Re: [dancer-users] Route dispatch 

Um, seems doesn't work. 


How can I add a "before hooks" in a Moo::Role ? 


----- Mail original -----

De: "Celogeek" <me at celogeek.com> 
À: "Perl Dancer users mailing list" <dancer-users at dancer.pm> 
Envoyé: Vendredi 28 Décembre 2012 20:23:36 
Objet: Re: [dancer-users] Route dispatch 

I'm trying right now on my apps (similar config) 


----- Mail original -----

De: "Alex C" <calyx238 at gmail.com> 
À: "Perl Dancer users mailing list" <dancer-users at dancer.pm> 
Envoyé: Vendredi 28 Décembre 2012 19:48:33 
Objet: Re: [dancer-users] Route dispatch 

Like this? 

package MyApp::Role::Hooks; 
hook before { var foo => 'bar' }; 

package MyApp::Routes::RouteA; 
with 'MyApp::Role::Hooks'; 
get '/A' => sub { return vars->{foo} }; 

package MyApp::Routes::RouteB; 
with 'MyApp::Role::Hooks'; 
get '/B' => sub { return vars->{foo} }; 

I hope that works because it looks elegant. 

On 28 December 2012 18:35, Celogeek < me at celogeek.com > wrote: 



Or we can push in a role the common part only between each module. 

For example, in my case, I have a common "before" hooks which initialize the redis connexion. So I can put this in a role, and that it. 


De: "Alex C" < calyx238 at gmail.com > 
À: "Perl Dancer users mailing list" < dancer-users at dancer.pm > 
Envoyé: Vendredi 28 Décembre 2012 19:28:41 
Objet: Re: [dancer-users] Route dispatch 

In my case, that would mean consuming 20+ roles into the same package, and I lose the benefit of package-scoping. 

I think there are many people who have spread their Dancer 1 routes across many packages. I would like to know the recommended best practice for migrating such apps to D2. 

On 28 December 2012 17:58, Celogeek < me at celogeek.com > wrote: 

<blockquote>


solution is to have only one Apps. 

And transform each submodule into a Moo::Role. 

may be 


De: "Alex C" < calyx238 at gmail.com > 
À: "Perl Dancer users mailing list" < dancer-users at dancer.pm > 
Envoyé: Vendredi 28 Décembre 2012 18:46:16 
Objet: Re: [dancer-users] Route dispatch 

Now I understand what you mean. I also separate my routes into differnet modules, like this: 

# in MyApp::Controller 

hook before { var foo => 'bar' }; 

get '/' => \&MyApp::Controller::Root; 

# MyApp::Controller::Root 

get '/' => sub { 
my $var = vars->{foo}; 
... 
}; 

I have yet to play with Dancer 2 but I expect I will have the same problems as you! 

I hope there is an easy solution to this or I cannot migrate to Dancer 2. 


On 28 December 2012 17:37, Celogeek < me at celogeek.com > wrote: 

<blockquote>

this doesn't work with Dancer2 due to context. 
the hook is for route in App::Main 
so it doesn't fire with the App::Root routes 



De: "Alex C" < calyx238 at gmail.com > 
À: "Perl Dancer users mailing list" < dancer-users at dancer.pm > 
Envoyé: Vendredi 28 Décembre 2012 18:33:57 
Objet: Re: [dancer-users] Route dispatch 

Sorry I have no experience with Dancer 2 or multiple contexts. 

This works on Dancer 1, if it is any help: 

#!/usr/bin/env perl 

use Dancer; 

{ 
package App::Main; 
use Dancer ':syntax'; 
hook before => sub { var xxx => "test" }; 
1; 
} 

{ 
package App::Root; 
use Dancer ':syntax'; 
get "/" => sub { return vars->{xxx} }; 
1; 
} 

start; 

Good luck.. 

On 28 December 2012 17:25, Celogeek < me at celogeek.com > wrote: 

<blockquote>

it's just a not working short example :) 

but Dancer use context, so in my App::Main I have a before hooks that only apply to the route present in App::Main. 

So my App::Root doesn't have it. And it was the case in Dancer 1. 

I need to fully change my apps to make it work with Dancer2 


De: "Alex C" < calyx238 at gmail.com > 
À: "Perl Dancer users mailing list" < dancer-users at dancer.pm > 
Envoyé: Vendredi 28 Décembre 2012 18:18:12 
Objet: Re: [dancer-users] Route dispatch 

Yuo are missing the semicolons after your sub declarations. Try it agian ;) 

On 28 December 2012 15:57, Celogeek < me at celogeek.com > wrote: 

<blockquote>

Can I dispatch my root thought multiple module ? 

I have in App::Main : 

hook before => sub { 
var xxx => "test"; 
} 

and in App::Root; 

get "/" => sub { 
return vars->{xxx} 
} 

But vars seems empty. 

In my bin/app.psgi I have : 

use Dancer; 
use App::Main; 
use App::Root; 

start; 



_______________________________________________ 
dancer-users mailing list 
dancer-users at dancer.pm 
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 






_______________________________________________ 
dancer-users mailing list 
dancer-users at dancer.pm 
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 


_______________________________________________ 
dancer-users mailing list 
dancer-users at dancer.pm 
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 


</blockquote>



_______________________________________________ 
dancer-users mailing list 
dancer-users at dancer.pm 
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 


_______________________________________________ 
dancer-users mailing list 
dancer-users at dancer.pm 
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 


</blockquote>



_______________________________________________ 
dancer-users mailing list 
dancer-users at dancer.pm 
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 


_______________________________________________ 
dancer-users mailing list 
dancer-users at dancer.pm 
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 


</blockquote>



_______________________________________________ 
dancer-users mailing list 
dancer-users at dancer.pm 
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 


_______________________________________________ 
dancer-users mailing list 
dancer-users at dancer.pm 
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 


</blockquote>



_______________________________________________ 
dancer-users mailing list 
dancer-users at dancer.pm 
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 


_______________________________________________ 
dancer-users mailing list 
dancer-users at dancer.pm 
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 


_______________________________________________ 
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/20121228/411b0484/attachment.htm>


More information about the dancer-users mailing list