我正在阅读
Mojolicious::Guides::Growing的部分,它告诉你如何将MojolicIoUs :: Lite发展成一个“组织良好”的cpan-uploadable应用程序.首先,它告诉您将M :: L应用程序拆分为启动脚本和应用程序类.
package MyApp; use Mojo::Base 'MojolicIoUs'; use MyUsers; sub startup { my $self = shift; # ...auth stuff omitted... my $r = $self->routes; $r->any('/' => sub { my $self = shift; my $user = $self->param('user') || ''; my $pass = $self->param('pass') || ''; return $self->render unless $self->users->check($user,$pass); $self->session(user => $user); $self->flash(message => 'Thanks for logging in.'); $self->redirect_to('protected'); } => 'index'); $r->get('/protected' => sub { my $self = shift; return $self->redirect_to('index') unless $self->session('user'); }); $r->get('/logout' => sub { my $self = shift; $self->session(expires => 1); $self->redirect_to('index'); }); } 1;
这对我来说很有意义.但接着说它可以进一步将这个应用程序类重构为带有动作的控制器类,并且应用程序类本身可以简化为路由信息:
package MyApp::Login; use Mojo::Base 'MojolicIoUs::Controller'; sub index { my $self = shift; my $user = $self->param('user') || ''; my $pass = $self->param('pass') || ''; return $self->render unless $self->users->check($user,$pass); $self->session(user => $user); $self->flash(message => 'Thanks for logging in.'); $self->redirect_to('protected'); } sub protected { my $self = shift; return $self->redirect_to('index') unless $self->session('user'); } sub logout { my $self = shift; $self->session(expires => 1); $self->redirect_to('index'); } 1; package MyApp; use Mojo::Base 'MojolicIoUs'; use MyUsers; sub startup { my $self = shift; # ...auth stuff omitted... my $r = $self->routes; $r->any('/')->to('login#index')->name('index'); $r->get('/protected')->to('login#protected')->name('protected'); $r->get('/logout')->to('login#logout')->name('logout'); } 1;
我不明白为什么这优于路由和动作混合的“混合”版本,因为现在为了在控制器中使用redirect_to()的动作之间重定向,你需要在不同的路由信息中查看文件,如果你想更改网址,你必须在两个不同的文件而不是一个文件中.这个:
$r->get('/protected' => sub { my $self = shift; return $self->redirect_to('index') unless $self->session('user'); });
变成:
sub protected { my $self = shift; return $self->redirect_to('index') unless $self->session('user'); } $r->get('/protected')->to('login#protected')->name('protected');
在两个不同的文件中有4次“受保护”一词(虽然我不确定名称(“受保护”)的用途).
顺便说一句,在Web开发方面,我是一个完全新手.
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。