使用"under"而不使用Mojolicious :: Lite的Mojolicious基本身份验证

Bor*_*pen 8 perl mojolicious

我正在寻找一个简洁的例子,说明如何在"Mojolicious"应用程序中使用"under"功能.我发现的所有例子都涉及"Mojolicious :: Lite"(我不使用).例如,我在这里听了http://mojocasts.com/e3的截屏视频,我想我理解了功能不足的概念.但我不使用"Mojolicious :: Lite",所以似乎我不能直接按照这个例子.我一直未能尝试采用Lite-example的非Lite风格.(这可能也是因为我仍然是框架的新手)

相关代码如下所示:

# Router
my $r = $self->routes;

# Normal route to controller
$r->get('/') ->to('x#a');
$r->get('/y')->to('y#b');
$r->any('/z')->to('z#c');
Run Code Online (Sandbox Code Playgroud)

因此,所有这些路由都需要通过user/pass保护.我试着这样做:

$r->under = sub { return 1 if ($auth) };
Run Code Online (Sandbox Code Playgroud)

但这不编译,我只是找不到匹配这个代码风格的例子......任何人都可以给我正确的提示或链接吗?请原谅我,如果这是在文档的某个地方...他们可能是完整的,但他们缺乏像我这样的简单头脑的人可以理解的例子:-P

Bor*_*pen 13

Lite-examples的类似代码如下所示:

# Router
my $r = $self->routes;

# This route is public
$r->any('/login')->to('login#form');

# this sub does the auth-stuff
# you can use stuff like: $self->param('password')
# to check user/pw and return true if fine
my $auth = $r->under( sub { return 1 } );

# This routes are protected
$auth->get ('/') ->to('x#a');
$auth->post('/y')->to('y#b');
$auth->any ('/z')->to('z#c');
Run Code Online (Sandbox Code Playgroud)

希望这对任何人都有帮助!

(此处的解决方案:http://mojolicio.us/perldoc/Mojolicious/Routes/Route#under)