加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 如何在一个mojolicious路线上添加多个over方法?

发布时间:2020-12-15 23:36:33 所属栏目:大数据 来源:网络整理
导读:我有以下代码: $r-find('user')-via('post')-over(authenticated = 1); 鉴于该路由,我可以通过经过身份验证的检查到达用户路由 这是使用Mojolicious :: Plugin :: Authentication设置的. 我想在那条路线上添加另一个“结束”. $r-find('user')-via('post')-
我有以下代码:

$r->find('user')->via('post')->over(authenticated => 1);

鉴于该路由,我可以通过经过身份验证的检查到达用户路由
这是使用Mojolicious :: Plugin :: Authentication设置的.

我想在那条路线上添加另一个“结束”.

$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');

但这根本不起作用.两者都没有被访问过.

我的路由是/ user,/ item,使用MojoX :: JSON :: RPC :: Service设置.
所以,我没有/ user /:id之类的东西来设置子路由.(不确定是否重要)
所有路由都像/ user,随参数一起发送.

我有一个像这样的条件:

$r->add_condition(
    access => sub {
        # do some stuff
    },);

这是$r-> route(‘/ user’) – > over(access => 1)中的’access’;

简而言之,使用时路线工作正常:

$r->find('user')->via('post')->over(authenticated => 1);

但是我无法添加第二条路线.

那么,在设置具有多个条件的这些路线时我缺少什么?
是否可以向单个路由/ route_name添加多个条件?

解决方法

您可以像在此测试中一样使用这两个条件:

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是对的,也许桥梁可以更好地完成工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读