Perl催化剂控制器重定向不起作用

时间:2016-09-08 17:34:42

标签: perl controller catalyst

我查看了这段代码,我无法理解它展示的奇怪之处。我所知道的缺乏理解

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

被忽略,如果 else 条件,则支持以下重定向。换句话说,我总是最终进入OAuthentication网站。

如果我阻止评论其他条件我最终会去哪里 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);
            }
    }
}

似乎我错过了一些关于它如何工作的基本原理。任何线索都赞赏

1 个答案:

答案 0 :(得分:3)

来自fine manual ...

  

这是一种便捷方法,可将Location标头设置为重定向目标,然后设置响应状态。如果您想直接进行重定向,则需要return$c->detach()来中断正常的处理流程。

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

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

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

取决于您的偏好。