Perl 催化剂 Controller 重定向不起作用

标签 perl controller catalyst

我查看了这段代码,但无法理解它表现出的怪异之处。因为不了解我所知道的一切

$c->res->redirect('qbo/home');

被忽略,支持以下 if else 条件中的重定向。换句话说,我总是在 OAuthentication 网站结束。

如果我阻止注释掉 else 条件,我最终会到达我想去的地方 qbo/home

sub index :Path :Args(0) {
    my ($self, $c) = @_;

    # Check to see if we have QBO::OAuth object in our user's session
    # Create new object in session if we don't already have one
    if(!($c->session->{qbo})) {
            $c->log->info('Creating QBO::OAuth, save in user session');
            $c->session->{qbo} = QBO::OAuth->new(
                    consumer_key => 'qyprddKpLkOclitN3cJCJno1fV5NzcT',
                    consumer_secret => 'ahwpSghVOzA142qOepNHoujyuHQFDbEzeGbZjEs3sPIc',
            );
    }

    # Now we set our object variable to the session old or new
    my $qbo = $c->session->{qbo};
    ######### GOTO 'qbo/home' ##########
    $c->res->redirect('qbo/home');
    ####################################
    if($c->req->params->{oauth_token}) {
            $c->log->info('Now Redirect to access_endpoint');
            # Get realmId and save it to our QBO::OAuth object in user session
            $qbo->realmId($c->req->params->{realmId});
            # Call QBO::OAuth->request_access_token
            my $r = $qbo->request_access_token($c->req->params->{oauth_verifier});
            $c->res->redirect('qbo/home');
    } else {
            my $callback = 'http://www.example.com/qbo';
            # Request a token
            my $r = $qbo->request_token($callback);
            if($qbo->has_token) {
                    #Continue on down, Redirect to auth_user_endpoint
                    $c->res->redirect($qbo->auth_user_endpoint . '?oauth_token=' . $qbo->token);
            }
    }
}

似乎我缺少有关其工作原理的一些基本原理。任何线索表示赞赏

最佳答案

来自fine manual ...

This is a convenience method that sets the Location header to the redirect destination, and then sets the response status. You will want to return or $c->detach() to interrupt the normal processing flow if you want the redirect to occur straight away.

另请注意该手册页上有关重定向到相对 URL 的警告 - 您不应该这样做。对于您的用例,我建议您养成使用的习惯:

return $c->res->redirect($c->uri_for('qbo/home'));

$c->res->redirect($c->uri_for('qbo/home')) && $c->detach();

取决于您的喜好。

关于Perl 催化剂 Controller 重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39397059/

相关文章:

perl - perlbrew和local::lib同时进行?

php - 在 Laravel 5.2 中将表单发送的参数传递给 Controller

移动应用程序中的 HTTP 到 HTTPS 重定向使用 MVC 4 用于某些页面,如登录、注册

perl - 为什么我的 Catalyst 应用程序无法读取我的 Template Toolkit 文件?

perl - 在 Perl/Catalyst 中通过 POST 实现多维和关联数组(哈希)

java - 我需要在 perl/java 中编写一段代码,在 1 秒内打印名称 100 次

perl - 当我访问 twitter 时 OAuth::Lite::Consumer 超时

perl - 在 Perl 中发送 SMTP 电子邮件的更快方法?

grails - 在事务 Controller Action 内部执行事务服务调用

javascript - Jquery 级联下拉菜单与 php 和 Catalyst