【问题标题】:How to get user info every time user access the pages of the site in zend?每次用户访问zend网站的页面时如何获取用户信息?
【发布时间】:2011-06-20 06:46:05
【问题描述】:

我是 Zend 的新手

我搜索了它并得到了以下链接, how to keep track of currently logged user in zend framework.

但我的要求不同。

我的情况是,我有一个名为 AuthenticationController 的单独控制器来使用 Zend_Auth 在会话中存储记录的用户详细信息并做其他事情,现在我必须获取记录的用户信息,当每次登录用户访问每个页面时网站。

所以我必须在每次触发的公共文件中执行一些逻辑才能在 zend 中获取用户信息。

如何在 zend 中做到这一点?

以下是示例代码..

if ($form->isValid($request->getPost())) {
    if ($this->_process($form->getValues())) {
        $authentication = Zend_Auth::getInstance();

        if ($authentication ->getIdentity()->myfirstlogin == 0) {

            $this->_helper->redirector('index', 'test1');

        } else {

            $this->_helper->redirector('index', 'test2');

        }
    } else {
        $myUserStatus = new Zend_Session_Namespace('myuserStatus');
        if ($myUserStatus ->status == 2) {
            $this->_helper->FlashMessenger('Incorrect Username / Password');
        } else if ($myUserStatus ->delete == 1) {
            $this->_helper->FlashMessenger('Your **** approval.');
        } else {
            $this->_helper->FlashMessenger('Please ****** administrator');
        }

        unset($myUserStatus);
        $this->_helper->redirector('index', 'auth');
    }
 }

.......

【问题讨论】:

  • 请分享一些代码...如果您没有以标准方式进行身份验证...如果没有任何代码,我们不可能知道您的情况。

标签: php zend-framework


【解决方案1】:

我相信你已经使用过Database Table Authentication 并且你有这样的stored your results。如果这是正确的。

您应该能够验证会话并获得这样的详细信息(在您想要的任何控制器中):

To Validate:

$sessionvars = Zend_Auth::getInstance()->getIdentity();
if (!Zend_Auth::getInstance()->hasIdentity()) {
    $this->_redirect('/');
}
...
...
To retrieve the data:

$userData = Zend_Auth::getInstance()->getIdentity();
print_r($userData);

【讨论】:

    【解决方案2】:

    我相信 OP 希望在一个公共文件中进行身份验证检查。我认为最好的选择是制作插件

    在库文件夹中创建一个新类。您可以选择使用不同的文件夹结构,但我在此示例中使用的文件夹结构是 library/Custom/Plugins/AuthCheck.php

    class Custom_Plugins_AuthCheck extends Zend_Controller_Plugin_Abstract
    {
        public function preDispatch (Zend_Controller_Request_Abstract $request)
        {
    
            // checking for identity. We want to avoid the check if this is the login form.
            if (($request->getModuleName() == 'default' && $request->getControllerName() == 'login')){
                 return;
            }
           else if(!Zend_Auth::getInstance()->hasIdentity()){ // if identity does not exist..redirect to login form
               $request->setModuleName('default')->setControllerName('login')->setActionName('index');
               $request->setDispatched(false);
            }
    
        }
    
    }
    

    请注意,我使用了一个名为 preDispatch 的函数。这不是一个随机的名称选择。我想在每次发送我的操作之前调用这段代码。 Zend Framework 在这个阶段有一个内置的插件调用,叫做 preDispatch()

    您可以在官方手册中阅读更多关于插件here的信息。

    现在我们的插件已经准备好了。我们必须注册这个插件,以便 Zend Framework 知道它必须在每次有 preDispatch() 插件调用时调用这个插件。

    我所知道的注册插件的最简单方法是将这段代码添加到您的 application.ini 中

    resources.frontController.plugins.AuthCheck = "Custom_Plugins_AuthCheck"
    

    现在您的插件将自动为每个请求调用。

    我希望我已经回答了你的问题。如果我错过了帖子中的某些内容,请告诉我。

    【讨论】:

    • 谢谢,我已经用自己的类尝试了我的逻辑,该类已放置在引导文件中,如下所示 class ModuleLayoutLoader extends Zend_Controller_Action_Helper_Abstract // 在 application.ini 中按模块查找布局 { public function preDispatch() { .. ... } }
    • 这将通过以下代码保护函数每次调用 _initLayoutHelper() { $this->bootstrap('frontController'); $layout = Zend_Controller_Action_HelperBroker::addHelper(new ModuleLayoutLoader()); }
    • 但是我必须用你的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 2022-07-31
    • 2014-09-03
    • 2020-11-18
    • 1970-01-01
    相关资源
    最近更新 更多