我来自perl Cgi,我开始使用mojolicious(和web框架),例如使用这个github: https://github.com/webdevesgi/mojolicious-demo
我有这个模板:
% layout 'menu2';
<hr/>
% if ( stash 'result' ) {
<li><%= stash 'result' %></li>
% } else {
<li>bar</li>
% }
<ul>
% foreach my $url (keys %$urls) {
<li><%= $urls->{$url}->{'url_origin'} %> - <a target="blank" href="http://localhost:3000/l/<%= $urls->{$url}->{'url_short'} %>">!<%= $urls->{$url}->{'url_short'} %></a> (<%= $urls->{$url}->{'clics'} %> clics)</li>
% }
</ul>
<br/>
<%= form_for sendurl => (method => 'post', class => 'well form-search' target = "_self") => begin %>
<p>Raccourcir une url :
<%= text_field 'orig_url', class => 'input-xlarge' %>
<%= submit_button 'GO', class => 'btn btn-warning' %>
</p>
<% end %>
我希望在做完帖子之后在这个模板中查看动作的结果,用以下几行:
% if ( stash 'result' ) {
<li><%= stash 'result' %></li>
% } else {
<li>bar</li>
% }
这是sendurl的代码:
sub sendurl{
my $self = shift;
my $url = Mojo::URL->new($self->param('orig_url'));
if(!$url->is_abs){
return $self->redirect_to('dashboard');
}
my $short_url = encodeurl($self->param('orig_url'));
$self->urls->addUrl($self->session('user'), $self->param('orig_url'), $short_url);
my $params = $self->req->params->to_hash;
if ($params) {
$self->stash( 'result' , "Errors - not sent" );
open F, q[>],'log.txt';print F Dumper $self;close F;
}
$self->redirect_to('dashboard', result=> "Errors - not sent");
}
在log.txt中,我可以看到存储具有值:
'stash' => {
'mojo.session' => {
'user_type' => 'admin',
'user' => 'foo'
},
'mojo.captures' => {
'controller' => 'login',
'action' => 'sendurl'
},
'menu' => $VAR1->{'app'}{'defaults'}{'config'}{'app_menu'}{'admin'},
'urls' => {
'' => {
'url_short' => '!532f1b4f',
'id' => undef,
'url_origin' => 'www.foo.bar/baz',
'clics' => undef
}
},
'mojo.active_session' => 2,
'mojo.started' => [
1395596194,
529165
],
'controller' => 'login',
'mojo.routed' => 1,
'action' => 'sendurl',
'mojo.secret' => 'login',
'config' => $VAR1->{'app'}{'defaults'}{'config'},
'result' => 'Errors - not sent'
},
但是在网络浏览器中,我无法看到结果的价值
我怎样才能做到这一点? 如何将sendurl的结果传递给视图,并在视图中检查存储中的结果变量? 这是与mojolicious合作的最佳方式?
答案 0 :(得分:2)
您可以直接以$result
的形式访问存储内容。请参阅http://mojolicio.us/perldoc/Mojolicious/Lite#Stash_and_templates上的文档。
背诵链接到的代码:
use Mojolicious::Lite;
# Route leading to an action that renders a template
get '/bar' => sub {
my $self = shift;
$self->stash(one => 23);
$self->render('baz', two => 24);
};
app->start;
__DATA__
@@ baz.html.ep
The magic numbers are <%= $one %> and <%= $two %>.
运行app
给出:
$ perl app.pl get /bar
[Sun Mar 23 23:20:32 2014] [debug] Your secret passphrase needs to be changed!!!
[Sun Mar 23 23:20:32 2014] [debug] GET "/bar".
[Sun Mar 23 23:20:32 2014] [debug] Routing to a callback.
[Sun Mar 23 23:20:32 2014] [debug] Rendering template "baz.html.ep" from DATA section.
[Sun Mar 23 23:20:32 2014] [debug] 200 OK (0.010140s, 98.619/s).
The magic numbers are 23 and 24.
$
答案 1 :(得分:2)
现在有时间仔细研究这些问题。如您所知,stash
条目无法在redirect_to
生效,但您可以使用flash
代替。
我对代码做了一些小改动,至少可以摆脱一些最特殊的东西。在这些更改之后,morbo
的调试输出看起来很清晰。
<强> LIB / Login.pm 强>
不是调用控制器操作来检查用户是否已登录,而是使用简单的回调:
my $logged_in = $r->under->to(cb => sub {
my $self = shift;
return defined $self->session('user');
});
<强> LIB /登录/ Login.pm 强>
将logged_in
子重命名为dashboard
(修复未找到的操作错误),并在最后添加适当的render
调用(以呈现login/dashboard
模板)。
sub dashboard {
my $self = shift;
$self->stash(
urls => $self->urls->getUrls($self->session('user')),
);
return $self->render;
}
在sub send_url
我将$self->stash(result =>
更改为flash
:
my $params = $self->req->params->to_hash;
if ($params) {
$self->flash(result => "Errors - not sent");
}
<强>模板/登录/ dashboard.html.ep 强>
您对result
的检查已成为:
<li><%= flash('result') // 'result not in flash' %></li>
还有一些最终的建议: