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.