Zend_Auth 快速且随处访问 UserID

发布于 2024-12-19 11:48:08 字数 281 浏览 2 评论 0原文

我有一个带有用户身份验证的 Zend 应用程序。我几乎在数据库查询的每个操作中都需要用户 ID。当然,我希望主要在控制器中快速访问,但也在视图脚本中快速访问。

到目前为止我的方式:

Zend_Auth::getInstance()->getIdentity()->User_ID

如何编写插件或帮助程序以更简单的方式提供 UserID?

例如这样:

$this->User_ID

I have a Zend Application with user authentication. I need the UserID in nearly every action for database queries. Certainly I want to have quick access primarily in controllers but also in view scripts.

The way I get so far:

Zend_Auth::getInstance()->getIdentity()->User_ID

How can I write a plugin or helper to provide the UserID in an easier way?

e.g. like this:

$this->User_ID

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港小镇 2024-12-26 11:48:08

您可以在控制器中创建可以在控制器构造函数中设置的属性:

$this->_User_ID = Zend_Auth::getInstance()->getIdentity()->User_ID;

或者在从那里建立和使用时将其放入 Zend_Registry 中。

you can make property in your controller which could be set in controller constructor:

$this->_User_ID = Zend_Auth::getInstance()->getIdentity()->User_ID;

or drop it to Zend_Registry when establish and use from there.

终止放荡 2024-12-26 11:48:08

这是一个插件,它将把用户 ID 注入到视图中,或者如果用户没有身份,则将其设置为 null。

<?php

class Application_Plugin_Identity extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $front     = Zend_Controller_Front::getInstance();
        $bootstrap = $front->getParam('bootstrap');

        $bootstrap->bootstrap('view');
        $view = $bootstrap->getResource('view');

        if (Zend_Auth::getInstance()->hasIdentity()) {
            $view->User_ID = Zend_Auth::getInstance()->getIdentity()->User_ID;
        } else {
            $view->User_ID = null;
        }
    }
}

要启用它,您可以在引导方法中注册它,如下所示:

Zend_Controller_Front::getInstance()
->registerPlugin(new Application_Plugin_Identity());

现在,从任何视图脚本中,您可以引用 $this->User_ID ,它将包含用户的 id,如果未记录,则它将包含 null更新

如果您主要想从控制器访问它,则操作助手可以很好地工作。

从您的控制器中,调用 $userId = $this->_helper->Identity(); 以获取用户 ID。作为奖励,我还将用户 ID 分配给视图,因此您可以从视图中调用 $this->User_ID

<?php

class My_Helper_Identity extends Zend_Controller_Action_Helper_Abstract
{
    protected static $_userId = null;

    public function direct()
    {
        if ($this->_userId == null) {
            $request  = $this->getRequest();
            $view     = $this->getActionController()->view;

            if (Zend_Auth::getInstance()->hasIdentity()) {
                $user_id = Zend_Auth::getInstance()->getIdentity()->User_ID;
            } else {
                $user_id = null;
            }

            $view->User_ID = $user_id;
            $this->_userId = $user_id;
            $this->getActionController()->_userId = $user_id;
        }

        return $this->_userId;
    }
}

Here is a plugin that will inject the user id into the view, or set it to null if the user has no identity.

<?php

class Application_Plugin_Identity extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $front     = Zend_Controller_Front::getInstance();
        $bootstrap = $front->getParam('bootstrap');

        $bootstrap->bootstrap('view');
        $view = $bootstrap->getResource('view');

        if (Zend_Auth::getInstance()->hasIdentity()) {
            $view->User_ID = Zend_Auth::getInstance()->getIdentity()->User_ID;
        } else {
            $view->User_ID = null;
        }
    }
}

To enable it you can register it in a bootstrap method like this:

Zend_Controller_Front::getInstance()
->registerPlugin(new Application_Plugin_Identity());

Now from any of your view scripts, you can reference $this->User_ID and it will contain the user's id or null if not logged in.

UPDATE:

An action helper works well if you mostly want to access it from your controller.

From your controller, call $userId = $this->_helper->Identity(); to get the user ID. As a bonus, I have it assign the user id to the view as well so from the view you can call $this->User_ID

<?php

class My_Helper_Identity extends Zend_Controller_Action_Helper_Abstract
{
    protected static $_userId = null;

    public function direct()
    {
        if ($this->_userId == null) {
            $request  = $this->getRequest();
            $view     = $this->getActionController()->view;

            if (Zend_Auth::getInstance()->hasIdentity()) {
                $user_id = Zend_Auth::getInstance()->getIdentity()->User_ID;
            } else {
                $user_id = null;
            }

            $view->User_ID = $user_id;
            $this->_userId = $user_id;
            $this->getActionController()->_userId = $user_id;
        }

        return $this->_userId;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文