views:

261

answers:

3

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-&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

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.

A: 

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;
Itay Moav
Calling a design pattern that you use 'vile' doesn't seem logical. Don't use it if you hate it
AntonioCS
@AntonioCS a sense of humor will not hurt you ;-)
Itay Moav
+2  A: 

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.

Gordon
A: 

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()

Matt McCormick