如何在View中设置当前用户?
我正在使用 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->_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
所以看起来数据库还没有被此时由框架设置。
必须有一种简单的方法来在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需在初始化 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.
我只是将用户置于最卑鄙的设计模式中,即单例模式,然后随时随地调用它。
或者
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.
OR
我最终在 Bootstrap 中添加了两个方法:
然后我就这样做:
在 _initViewHelpers() 中
I ended up adding two methods to the Bootstrap:
Then I just do:
in _initViewHelpers()