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