Hi Mojolicious users,

i came across something strange when trying to play with routing and 
dispatcher.

In the documentation, in that section: 
http://mojolicious.org/perldoc/Mojolicious/Guides/Routing#Optional-placeholders 
, we see:

Special stash values like controller and action can also be placeholders, 
which is very convenient especially during development, but should only be 
used very carefully, because every controller method becomes a potential 
route. All uppercase methods as well as those starting with an underscore 
are automatically hidden from the router and you can use “hide” in 
Mojolicious::Routes to add additional ones.

I create a small app to show case what could be an issue either in the 
documentation or in the code itself.

   - lib/App.pm 

package App;use Mojo::Base 'Mojolicious';
sub startup {
    my $app = shift;

    my $r = $app->routes;

    $r->get('/in_url/:controller/:action')->to( controler => 'first', action => 
'fallback' );

    $r->under( '/in_stash' => sub {
        my $c = shift;
        $c->stash( controller => 'second', action => 'index' );
        return 1;
    } )->get('test')->to( controller => 'first', action => 'fallback' );
}1;


   - lib/App/Controller/First.pm 

package App::Controller::First;use Mojo::Base 'Mojolicious::Controller';
sub index { my $c = shift; $c->render( text => 'Index : 
'.$c->stash('controller').' / '.$c->stash('action') ); }sub other { my $c = 
shift; $c->render( text => 'Other : '.$c->stash('controller').' / 
'.$c->stash('action') ); }sub fallback { my $c = shift; $c->render( text => 
'Fallback : '.$c->stash('controller').' / '.$c->stash('action') ); }
1;


   - lib/App/Controller/Second.pm 

package App::Controller::Second;use Mojo::Base 'Mojolicious::Controller';
sub index { my $c = shift; $c->render( text => 'Index 2 : 
'.$c->stash('controller').' / '.$c->stash('action') ); }sub other { my $c = 
shift; $c->render( text => 'Other 2 : '.$c->stash('controller').' / 
'.$c->stash('action') ); }sub fallback { my $c = shift; $c->render( text => 
'Fallback 2 : '.$c->stash('controller').' / '.$c->stash('action') ); }
1;

The routes under /in_url are taking controller and action from the 
placeholder in the url, and according to the documentation from the stash, 
it’s working as expected:

» curl http://localhost:3000/in_url/first
Fallback : first / fallback
» curl http://localhost:3000/in_url/second/other
Other 2 : second / other

However, with the second route, where set the stash within the under part 
of the route:

curl http://localhost:3000/in_stash/test
Fallback : first / fallback

The stash actually got rewritten, which means that the stash values that i 
set in the under are rewritten with the values from the to

When i look at the code, in Mojolicious/Routes.pm, it seems that the 
controller and class are taken from the route itself and not from the 
stash, and after that, those value are pushed to the stash. So the value 
from the placeholders are taking precedence on the default ones set in the 
to part of the route, however, the one from the stash get discarded

Am i getting something wrong? That was not my understanding from the 
documentation.

Pierre
​

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to