Perl 催化剂控制器重定向不起作用
Perl catalyst controller redirect not working
我查看了这段代码,但无法理解它表现出的怪异之处。由于缺乏了解我所知道的
$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();
取决于您的喜好。
我查看了这段代码,但无法理解它表现出的怪异之处。由于缺乏了解我所知道的
$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();
取决于您的喜好。