Yes there are two recommended solutions, first one is to make a plugin (http://framework.zend.com/manual/1.12/en/zend.controller.plugins.html) that gets loaded for example in your bootstrap and will do something for each request, if what you want to do applies only to a few actions you could create an action helper (http://framework.zend.com/manual/1.12/en/zend.controller.actionhelpers.html).
In your case i would create a frontController plugin for example on dispatchLoopStartup that stores the user data in the registry, then in your view you retrieve that data from the registry and echo it. To reduce the amount of db requests, the plugin could use the user session to store the informations, so only the first request to your site would trigger a db query, or put the user data in the session upon login.
I dont recommend using a baseController as it is not a best practice and you would have to edit all your controllers to tell them to extend the baseController, i will also not use the init function of all your controllers for the same reason, by using the plugin all you need to do is initialize it in your boostrap and then retrieve the data from registry in your header view partial
the name of the plugin starts with the name of your library directory, followed by any subfolders that you may have and after the last underscore is the name of your class, this is why the autoloader finds your plugin by replacing the underscores with slahes or backslashes depending on your OS to get the correct path
header.phtml:
<?php
// test if userData is there
Zend_Debug::dump($userData);
Bootstrap.php:
<?php
// add the namespace of your library directory
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyLibrary_');
// register header plugin
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new headerUserDataPlugin());
Put the following plugin into the correct folder and adapt the plugin class name if your directories structure differs from this one, for example your Library folder is maybe not called "myLibrary":
application/
configs/
application.ini
forms/
layouts/
modules/
views/
Bootstrap.php
library/
Zend/
MyLibrary/
Controller/
Plugin/
HeaderUserDataPlugin.php
public/
index.php
HeaderUserDataPlugin.php:
<?php
/**
* header user data plugin
*
**/
class MyLibrary_Controller_Plugin_HeaderUserDataPlugin extends Zend_Controller_Plugin_Abstract {
public function routeShutdown(Zend_Controller_Request_Abstract $request) {
// at this point you can retrieve the user id from session
$mysession = new Zend_Session_Namespace('mysession');
// is there a userId in the session
if (!isset($mysession->userId)) {
$userId = $mysession->userId;
// get userData from user model
$userModel = new UserModel();
$userData = UserModel->getUserData($mysession->userId);
Zend_Registry::set('user_data', $userData);
}
}
}
I did not test the code, there may be some typos :S