php - 重定向到路由在 Zend Framework 中不起作用

标签 php zend-framework2 google-oauth

我的应用程序使用 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/

相关文章:

c# - ASP.NET Core Web API + 对预检请求的 Angular 响应未通过访问控制检查 : Redirect is not allowed for a preflight request

node.js - NodeJS,如何使用 google api 使用刷新 token 获取新 token ?

php - 通过 htaccess 为登录用户强制 https

php - 引用 - 这个错误在 PHP 中意味着什么?

php - 内联 PHP 克隆

zend-framework2 - ZF3 - EventManager 和调度事件

zend-framework2 - 如何从Zend Framework 2(zf2)中的View访问应用程序配置?

php - ZF2 2.2.4 翻译器问题

django - create_user() 缺少 1 个必需的位置参数 : 'password'

php - 一个 php 帖子的最大大小?