如何在View中设置当前用户?

发布于 2024-08-17 04:19:33 字数 2414 浏览 8 评论 0原文

我正在使用 Zend 框架。

我希望当前用户(如果已登录)始终可以查看。在我的 Bootstrap.php 文件中,我目前有:

function _initViewHelpers()
{
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();

            // <snip> $view-> set some stuff

    $view->identity = Zend_Auth::getInstance()->getIdentity();
}

我的 index.php 文件看起来像:

require_once 'Zend/Application.php';  

// Create application, bootstrap, and run
$application = new Zend_Application(APPLICATION_ENV, 
    APPLICATION_PATH . '/configs/application.ini');

$application->bootstrap();

$tblUser = new Model_DbTable_User();
Zend_Registry::set('user', $tblUser->getUser());

$application->run();

我希望 Bootstrap 是这样的:

function _initViewHelpers()
{
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();

    $tblUser = new Model_DbTable_User();
    $user = $tblUser->getUser();
    Zend_Registry::set('user', $user);

    $view->user= $user;
}

但是,如果我尝试这样做,我会得到一个错误:

Fatal error:  Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for Model_DbTable_User' in /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Db/Table/Abstract.php:754
Stack trace:
#0 /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Db/Table/Abstract.php(739): Zend_Db_Table_Abstract-&gt;_setupDatabaseAdapter()
#1 /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Db/Table/Abstract.php(268): Zend_Db_Table_Abstract-&gt;_setup()
#2 /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Db/Table.php(77): Zend_Db_Table_Abstract-&gt;__construct(Array)
#3 /var/www/application/Bootstrap.php(25): Zend_Db_Table-&gt;__construct()
#4 /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Application/Bootstrap/BootstrapAbstract.php(662): Bootstrap-&gt;_initViewHelpers()
#5 /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Application/Bootstrap/BootstrapAbstract.php(615): Zend_Application_Bootstrap_BootstrapAbstract-&gt;_executeResource('viewhelpers')
#6 /usr/local/php/ZendFramework-1.9.5- in /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Db/Table/Abstract.php on line 754

所以看起来数据库还没有被此时由框架设置。

必须有一种简单的方法来在 Zend Framework 中设置当前用户对象,但我无法弄清楚。

I am using Zend Framework.

I want the current user (if logged in) to always be available to the view. In my Bootstrap.php file I currently have:

function _initViewHelpers()
{
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();

            // <snip> $view-> set some stuff

    $view->identity = Zend_Auth::getInstance()->getIdentity();
}

And my index.php file looks like:

require_once 'Zend/Application.php';  

// Create application, bootstrap, and run
$application = new Zend_Application(APPLICATION_ENV, 
    APPLICATION_PATH . '/configs/application.ini');

$application->bootstrap();

$tblUser = new Model_DbTable_User();
Zend_Registry::set('user', $tblUser->getUser());

$application->run();

I want the Bootstrap to be something like this:

function _initViewHelpers()
{
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();

    $tblUser = new Model_DbTable_User();
    $user = $tblUser->getUser();
    Zend_Registry::set('user', $user);

    $view->user= $user;
}

If I try this, though, I get an error:

Fatal error:  Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for Model_DbTable_User' in /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Db/Table/Abstract.php:754
Stack trace:
#0 /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Db/Table/Abstract.php(739): Zend_Db_Table_Abstract->_setupDatabaseAdapter()
#1 /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Db/Table/Abstract.php(268): Zend_Db_Table_Abstract->_setup()
#2 /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Db/Table.php(77): Zend_Db_Table_Abstract->__construct(Array)
#3 /var/www/application/Bootstrap.php(25): Zend_Db_Table->__construct()
#4 /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Application/Bootstrap/BootstrapAbstract.php(662): Bootstrap->_initViewHelpers()
#5 /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Application/Bootstrap/BootstrapAbstract.php(615): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource('viewhelpers')
#6 /usr/local/php/ZendFramework-1.9.5- in /usr/local/php/ZendFramework-1.9.5-minimal/library/Zend/Db/Table/Abstract.php on line 754

So it looks like the database has not been setup by the framework at this point.

There must be a simple way to set the current user object in Zend Framework but I can't figure it out.

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

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

发布评论

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

评论(3

鹤仙姿 2024-08-24 04:19:33

只需在初始化 Bootstrap 中的视图之前初始化数据库,或者在调用 _initViewHelpers() 时设置数据库适配器即可。不过,我会使用 控制器插件

Just initialize the DB before your initialize the View in your Bootstrap or set the DB adapter when you call _initViewHelpers(). I'd use a Controller Plugin though.

甚是思念 2024-08-24 04:19:33

我只是将用户置于最卑鄙的设计模式中,即单例模式,然后随时随地调用它。

bl_User::getInstance()->user_id;
bl_User::getInstance()->user_name;

或者

$User=bl_User::getInstance();bl_User::getInstance()
$User->user_id;
$User->user_name;

I simply put the user in the most vile of design patterns, the singleton, and call it when ever and where ever I want it.

bl_User::getInstance()->user_id;
bl_User::getInstance()->user_name;

OR

$User=bl_User::getInstance();bl_User::getInstance()
$User->user_id;
$User->user_name;
长梦不多时 2024-08-24 04:19:33

我最终在 Bootstrap 中添加了两个方法:

function _initDatabase()
{
    $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 
        APPLICATION_ENV);
    $db = Zend_Db::factory($config->resources->db->adapter, 
        $config->resources->db->params);
    Zend_Registry::set('db', $db);
}

function _initUser()
{
    $tblUser = new Model_DbTable_User(array('db' => Zend_Registry::get('db')));
    $user = $tblUser->getUser();
    Zend_Registry::set('user', $user);
}

然后我就这样做:

$view->user = Zend_Registry::get('user');

在 _initViewHelpers() 中

I ended up adding two methods to the Bootstrap:

function _initDatabase()
{
    $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 
        APPLICATION_ENV);
    $db = Zend_Db::factory($config->resources->db->adapter, 
        $config->resources->db->params);
    Zend_Registry::set('db', $db);
}

function _initUser()
{
    $tblUser = new Model_DbTable_User(array('db' => Zend_Registry::get('db')));
    $user = $tblUser->getUser();
    Zend_Registry::set('user', $user);
}

Then I just do:

$view->user = Zend_Registry::get('user');

in _initViewHelpers()

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