Monday, April 17, 2006

Catalyst and $c->res->redirect

今天发现关于 $c->res->redirect 的一件事。post 在这 http://lists.rawmode.org/pipermail/catalyst/2006-April/006758.html
sub test : Global {
my ( $self, $c ) = @_;
unless ($c->user_exists) {
$c->res->redirect('/login');
# return 1;
}
$c->res->body($c->user->user_id);
}
注意这里的 $c->res->redirect, 它不是马上一碰到就执行的,而是全部执行完整个 test 子程序才执行的。
也就是说,上面的代码是错误的。当用户没有登陆了,虽然要执行 $c->res->redirect, 但是它还要去执行 $c->res->body($c->user->user_id); 而不是一碰到 redirect 就直接跳转。所以代码会在 $c->res->body($c->user->user_id); 这句出错。因为没登陆时是不能访问 $c->user->user_id 的。把 return 1; 的注释去掉就可以了。

写代码事要注意这件事。:)

1 Comments:

Anonymous chunzi said...

why not just:

return $c->res->redirect(...);

:)

4/17/2006 10:38 PM  

Post a Comment

<< Home