为什么 Catalyst 中的条件重定向不起作用?

发布于 2024-10-29 07:22:10 字数 639 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木森分化 2024-11-05 07:22:10

也许您陷入了无限循环,其中您的 begin 子将用户重定向到 Catalyst 应用程序中的另一个页面;一旦“将运行的控制器已被识别,但在调用任何 URL 匹配操作之前”(来自 Catalyst::Manual::Intro 手册页),begin 将被再次调用,导致另一次重定向并很快。

尝试将此代码完全移出 begin ;也许,正如 Htbaa 所建议的,auto 可能就是您正在寻找的。标准 $c->detach 情况(在控制器 controller 中)是:

sub check_login :Local {
  # do something
  $c->detach('controller/login_successful') if($success);
  # display error message
}

sub login_successful :Local {
  # do something with the logged in user.
}

在这种情况下,执行 $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 the Catalyst::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 controller controller) is:

sub check_login :Local {
  # do something
  $c->detach('controller/login_successful') if($success);
  # display error message
}

sub login_successful :Local {
  # do something with the logged in user.
}

In this case, doing a $c->res->redirect('http://example.com/login_successful') should work perfectly as well. Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文