我有以下代码:
$r->find('user')->via('post')->over(authenticated => 1);
Run Code Online (Sandbox Code Playgroud)
鉴于该路由,我可以通过使用Mojolicious :: Plugin :: Authentication设置的经过身份验证的检查到达用户路由.
我想在那条路线上添加另一个"结束".
$r->find('user')->via('post')->over(authenticated => 1)->over(access => 1);
Run Code Online (Sandbox Code Playgroud)
这似乎覆盖了经过身份验证的'over'.
我尝试用以下名称分解路线:
my $auth = $r->route('/')->over(authenticated => 1)
->name('Authenticated Route');
$access = $auth->route('/user')->over(access => 1)->name('USER_ACCESS');
Run Code Online (Sandbox Code Playgroud)
但这根本不起作用.两者都没有被访问过.
我的路由是/ user,/ item,使用MojoX :: JSON :: RPC :: Service设置.所以,我没有/ user /:id之类的东西来设置子路由.(不确定是否重要)所有路由都像/ user,带参数发送.
我有一个像这样的条件:
$r->add_condition(
access => sub {
# do some stuff
},
);
Run Code Online (Sandbox Code Playgroud)
这是$ r-> route('/ user') - > over(access => 1)中的'access';
简而言之,使用时路线工作正常:
$r->find('user')->via('post')->over(authenticated => 1);
Run Code Online (Sandbox Code Playgroud)
但是我无法添加第二条路线.
那么,在设置具有多个条件的这些路线时我缺少什么?是否可以向单个路由/ route_name添加多个条件?
您可以over在此测试中使用类似的两个条件:
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
Run Code Online (Sandbox Code Playgroud)
在 Perl 5.12.1 上与 Mojolicious 3.38 一起工作得很好 - @DavidO 是对的,也许桥接器可以做得更好。:)