I have the following code:
$r->find('user')->via('post')->over(authenticated => 1);
Given that route I can get to the user route passing through the authenticated check that is setup using Mojolicious::Plugin::Authentication.
I want add another 'over' to that route.
$r->find('user')->via('post')->over(authenticated => 1)->over(access => 1);
That appears to override authenticated 'over' though.
I tried breaking up the routes with names like:
my $auth = $r->route('/')->over(authenticated => 1)
->name('Authenticated Route');
$access = $auth->route('/user')->over(access => 1)->name('USER_ACCESS');
That didn't work at all though. Neither of the 'over's are being accessed.
My routes are things like /user, /item, set up using MojoX::JSON::RPC::Service. So, I don't have things like /user/:id to set up sub routes.( not sure that matters ) All routes are like /user, sent with parameters.
I've got a condition like:
$r->add_condition(
access => sub {
# do some stuff
},
);
that is the 'access' in $r->route('/user')->over(access => 1);
In short, the routes work fine when using:
$r->find('user')->via('post')->over(authenticated => 1);
But I'm unable to add a 2nd route.
So, what am I missing in setting up these routes with multiple conditions? Is it possible to add multiple conditions to a single route /route_name?
After you start a new route with methods like "get" in Mojolicious::Routes::Route, you can also give it a destination in the form of a hash using the chained method "to" in Mojolicious::Routes::Route. Now if the route matches an incoming request it will use the content of this hash to try and find appropriate code to generate a response.
There are already shortcuts for the most common HTTP request methods like "post" in Mojolicious::Routes::Route, and for more control "any" in Mojolicious::Routes::Route accepts an optional array reference with arbitrary request methods as first argument.
We can define as many route parameters or placeholders in the Mojolicious routes. Conventional dynamic route parameters start with ":" and consist of alphabetic characters. Underscores (_) are also acceptable within route parameter names. Placeholders use a colon prefix and match all characters except / and ., similar to the regular expression.
Most commonly used features every Mojolicious developer should know about. The attribute "routes" in Mojolicious contains a router you can use to generate route structures. The minimal route above will load and instantiate the class MyApp::Controller::Foo and call its welcome method.
You can just use both conditions in over
like in this test:
use Mojolicious::Lite;
# dummy conditions storing their name and argument in the stash
for my $name (qw(foo bar)) {
app->routes->add_condition($name => sub {
my ($route, $controller, $to, @args) = @_;
$controller->stash($name => $args[0]);
});
}
# simple foo and bar dump action
sub dump {
my $self = shift;
$self->render_text(join ' ' => map {$self->stash($_)} qw(foo bar));
}
# traditional route with multiple 'over'
app->routes->get('/frst')->over(foo => 'yo', bar => 'works')->to(cb => \&dump);
# lite route with multiple 'over'
get '/scnd' => (foo => 'hey', bar => 'cool') => \&dump;
# test the lite app above
use Test::More tests => 4;
use Test::Mojo;
my $t = Test::Mojo->new;
# test first route
$t->get_ok('/frst')->content_is('yo works');
$t->get_ok('/scnd')->content_is('hey cool');
__END__
1..4
ok 1 - get /frst
ok 2 - exact match for content
ok 3 - get /scnd
ok 4 - exact match for content
Works fine here with Mojolicious 3.38 on perl 5.12.1 - @DavidO is right, maybe bridges can do the job better. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With