为什么 Catalyst 中的条件重定向不起作用?
我有一个 Catalyst 应用程序,希望根据条件语句进行重定向。我在这方面遇到了麻烦,我想知道是否有人能够深入了解为什么这项看似简单的任务事实证明很困难。
在我的 Root.pm
模块中,我有一个子 begin
,可以重定向到另一个网站,例如 www.perl.org
,但我无法重定向到我的应用程序中的页面。关于如何进行条件重定向有什么想法吗?
sub begin : Private {
my ( $self, $c ) = @_;
$c->stash->{client_id} = somenumber; # I'm setting this manually for testing
$c->res->redirect('http://www.perl.org/') unless $c->stash->{client_id};
$c->res->redirect('http://www.mysite.com/success') if $c->stash->{client_id}; #does not
}
I have a Catalyst application and would like to redirect based on a conditional statement. I am having trouble with this and I'm wondering if anyone might have insight into why this seemingly easy task is proving difficult.
In my Root.pm
module I have a sub begin
and can redirect to another website, e.g. www.perl.org
, but I am unable to redirect to a page within my application. Any thoughts on how to do a conditional redirect?
sub begin : Private {
my ( $self, $c ) = @_;
$c->stash->{client_id} = somenumber; # I'm setting this manually for testing
$c->res->redirect('http://www.perl.org/') unless $c->stash->{client_id};
$c->res->redirect('http://www.mysite.com/success') if $c->stash->{client_id}; #does not
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您陷入了无限循环,其中您的
begin
子将用户重定向到 Catalyst 应用程序中的另一个页面;一旦“将运行的控制器已被识别,但在调用任何 URL 匹配操作之前”(来自Catalyst::Manual::Intro
手册页),begin
将被再次调用,导致另一次重定向并很快。尝试将此代码完全移出
begin
;也许,正如 Htbaa 所建议的,auto
可能就是您正在寻找的。标准$c->detach
情况(在控制器controller
中)是:在这种情况下,执行
$c->res->redirect( 'http://example.com/login_successful')
也应该可以完美工作。希望有帮助!Maybe you're getting stuck in an infinite loop, in which your
begin
sub redirects the user to another page in your Catalyst application; once "the controller that will run has been identified, but before any URL-matching actions are called" (from theCatalyst::Manual::Intro
man page),begin
will be called again, causing another redirect and so on.Try moving this code out of
begin
entirely; perhaps, as Htbaa suggested,auto
might be what you're looking for. The standard$c->detach
case (in controllercontroller
) is:In this case, doing a
$c->res->redirect('http://example.com/login_successful')
should work perfectly as well. Hope that helps!