我的应用程序使用 Zend Framework 2.2.4,我为登录页面添加了“使用 Google 登录”选项。但它并没有在确切的页面上重定向,它返回到登录页面并放置一个 #
例如 myapp.dev/login#
这是“使用 Google 登录”的方法,位于我的 LoggingController.php
public function loginSocialsAction()
{
$googleClient = $this->getGoogleClient();
$code = $this->params()->fromQuery('code', '');
$googleOauthV2 = new \Google_Oauth2Service($googleClient);
#if successful validation from Google
if (isset($code)) {
$googleClient->authenticate($code);
$gdata = $googleOauthV2->userinfo->get();
$user = $this->getUsersTable()->getByEmail($gdata['email']);
}
if ($googleClient->getAccessToken() && isset($gdata['email'])) {
if (!empty($user)) {
$user->role = $this->getRoleTable()->get($user->roleId);
$session = new Session\Container('currentuser');
$session->user = $user;
$this->initializeSession($session);
// if success, go to records page
return $this->redirect()->toRoute('records');
} else {
$message = 'The user is not in the system.';
$this->flashMessenger()->setNamespace('error')->addErrorMessage($message);
return $this->redirect()->toRoute('login');
}
} else {
$message = 'Google auth failed.';
$this->flashMessenger()->setNamespace('error')->addErrorMessage($message);
return $this->redirect()->toRoute('login');
}
}
请提及我错过的或您想知道的事情。任何想法都会有帮助!
最佳答案
你可以试试这个重定向。
用于 Controller 重定向
return $this->redirect()->toUrl($this->getRequest()->getBaseUrl().'/any/path/here/'.<<ID HERE, IF YOU WANT>>);
用于 View 重定向
在这种情况下,点击按钮取消会重定向,重置值。
<button type="reset"
<a href="<?php echo $this->basePath('your/path/here/'. <<ID HERE IF ANY>> ) ?>">Cancel
</a>
</button>
可以看出明显的区别,因为 controller 的 URL 是使用 getBaseUrl()
重定向的,而 view 的 URL 是使用 basepath 重定向的()
。
关于php - 重定向到路由在 Zend Framework 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645200/