zend-framework - Zend 框架 : How to redirect to original url after login?

标签 zend-framework redirect authentication

我正在尝试实现一个登录系统,该系统足够智能,可以在用户决定(或被迫)进入登录页面之前将用户重定向回他们所在的页面。

我知道这似乎是一个与 this one 类似的问题, 和 this one ,但它们并未解决我的两种情况。

这里有两种情况:

  1. 用户特别决定去登录页面:

    <a href="<?php echo $this->url(array(
        'controller'=>'auth',
        'action'=>'login'), 'default', true); ?>">Log In</a>
    
  2. 用户被重定向,因为他们试图访问 protected 内容:

    if (!Zend_Auth::getInstance()->hasIdentity()) {
        $this->_helper->redirector('login', 'auth');
    }
    

如何在不在地址栏中显示“重定向到”url 的情况下实现解决方案?

最佳答案

在 session 中保存目标 URL。我猜你有某种访问预调度插件。在那里做。然后,在登录表单处理程序中,检查 session 中的目标 URL,并在身份验证成功后重定向到它。

我项目中的示例代码:

class Your_Application_Plugin_Access extends Zend_Controller_Plugin_Abstract {
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        foreach (self::current_roles() as $role) {
            if (
                Zend_Registry::get('bootstrap')->siteacl->is_allowed(
                    $role,
                    new Site_Action_UriPath($request->getPathInfo())
                )
            ) return; // Allowed
        }

        $this->not_allowed($request);
    }

    private function not_allowed(Zend_Controller_Request_Abstract $request) {
        $destination_url = $request->getPathInfo();

        // If the user is authenticted, but the page is denied for his role, show 403
        // else,
        // save $destination_url to session
        // redirect to login page, with $destination_url saved:
        $request
            ->setPathInfo('/login')
            ->setModuleName('default')
            ->setControllerName('login')
            ->setActionName('index')
            ->setDispatched(false);
    }

    ...

}

这里,current_roles() 总是包含 'guest',它是未经身份验证的用户,Zend_Auth::hasIdentity() 是 false .

关于zend-framework - Zend 框架 : How to redirect to original url after login?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1984352/

相关文章:

vue.js - 如何使用 Nuxt 中间件重定向到外部站点?

java - 在 Java 中重定向一个 ip

php - Yii2 注销管理部分无法正常工作

bash - 无法验证应用程序默认凭据

php - 如何检测 iPhone 是否具有视网膜显示屏?

zend-framework - Zend Paginator与Gdata Youtube

zend-framework - zend 形式的数组具有相同的索引

php - 谁能提供一些有关 Zend Framework 中表单单元测试的资源?

Javascript:重定向到不同的子域

android - 如何在智能卡读卡器中实现被动认证?