I use Zend_Auth to authenticate users and then store their details in the default Zend_Auth session. This means that when a user edits his details, these changes won’t be reflected in the application until he re-authenticates.
I want to avoid this problem as so:
- When the user logs in we only store his
user IDin aZend_Authsession -
On each request we fetch the user’s details from the database in a
preDispatch()hook, using theuser IDwhich was stored upon login in theZend_Authsession:class Plugin_Auth extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { if ($auth->hasIdentity()) { $id = $auth->getIdentity()->id; $userModel = new Model_User(); $user = $userModel->fetchOne($id); // Where do I store this user object ??? } } } -
The problem is: where do i store this
Userobject? I think we shouldn’t use sessions for this, since the goal of sessions is to persist data. There’s no need for persistence though, since we re-fetch the data from the database on each request. Only theuser IDmust be persistent. Would storing theUserobject inZend_Registrybe an option here?
Use
Zend_Session_Namespaceto store the object. It can be as temporary or permanent as you wish to make it.Zend_Auth already uses this in the background as it’s default storage mechanism using the namespace of
Zend_Auth.Of course Zend_Registry will work as well and as always the choice is yours. You may even find it appropriate to build this functionality into your auth adapter.