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

时间:2010-01-09 17:25:07

标签: php zend-framework zend-db zend-view

我正在使用Zend Framework。

我希望当前用户(如果已登录)始终可供视图使用。在我的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中设置当前用户对象,但我无法弄明白。

3 个答案:

答案 0 :(得分:2)

在初始化Bootstrap中的View之前初始化数据库,或者在调用_initViewHelpers()时设置数据库适配器。我虽然使用Controller Plugin

答案 1 :(得分:0)

我只是将用户置于最卑鄙的设计模式中,即单身人士,并在任何时候和我想要的地方调用它。

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

OR

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

答案 2 :(得分:0)

我最终在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()

中的