dongpo0409 2015-11-11 06:15
浏览 52

重定向到路由不在Zend Framework中工作

I'm using Zend Framework 2.2.4 for my app, I added a 'Sign-in with Google' option for the login page. But it's not redirecting on the exact page, it goes back to the login page and places a #

e.g. myapp.dev/login#

This is the method for the 'Sign-in with Google', this is located at my 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');
    }
}

Please mention the things that I missed or you want to know. Any idea(s) would really help!

  • 写回答

1条回答 默认 最新

  • dqwyghl0649 2017-01-27 07:55
    关注

    You can try this for redirection.

    For Controller Redirection

    return $this->redirect()->toUrl($this->getRequest()->getBaseUrl().'/any/path/here/'.<<ID HERE, IF YOU WANT>>);
    

    For View Redirection

    In this case, a button Cancel when clicked redirects, resetting the values.

    <button type="reset"   
       <a  href="<?php echo $this->basePath('your/path/here/'. <<ID HERE IF ANY>> ) ?>">Cancel
             </a>
    </button>
    

    The obvious difference can be seen, since controller's URLs are redirected using getBaseUrl() while view's URLs are redirected using basepath().

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法