This video covers the Zend_Session_Namespace class. It’s a short introduction to how we can use this object to simplify passing data from Zend_Controller action to another during a user’s stay on our web application.
Managing Session with Zend_Session_Namespace
Description
This video covers the Zend_Session_Namespace class. It’s a short introduction to how we can use this object to simplify passing data from Zend_Controller action to another during a user’s stay on our web application.
Tags
actions, session, zend_controller, zend_session_namespace

A look at the IndexController.php file:
/**
* Description of IndexController
*
* @author zendcasts
*/
class IndexController extends Zend_Controller_Action
{
public function preDispatch()
{
$this->session = new Zend_Session_Namespace(‘default’);
if (isset($this->session->pageCounter))
$this->session->pageCounter++;
else
$this->session->pageCounter = 0;
}
public function indexAction()
{
$this->view->message = $this->session->pageCounter;
}
public function pagetwoAction()
{
$this->session->pageCounter = $this->session->pageCounter + 10;
$this->view->message = $this->session->pageCounter;
}
}
Hi,
excellent tutorial!
But could you explain why the preDispatch mathod is necessary?
I doesn’t work if I instantiate the session in another method but why?
Regards
Hi Jay,
preDispatch is the first method that’s called within the controller. instantiating session there is best since it occurs before any rendering of the controller takes place. Also, you have access to the Request Object by that point as well.
See http://framework.zend.com/manual/en/zend.controller.basics.html for details, let me know if you have any more questions.
Cheers
The example works fine.
I am trying to use the session variables nested.
When the sample code is applied in this structure:
application
modules
admin
controllers
views
This gives an exception.
I can use session[] which gives a warning.
What am I missing?
Correct me if I’m wrong, but my experimentation shows that if I am using the ActionStack to trigger some additional actions that reside in the index controller, they are fired on postDispatch, which will cause the preDispatch in your example to get fired multiple times.
I noticed this since my counter was incrementing by 3 each time. A check of my ActionStack plugin showed that my 2 xtra plugins were causing this.
Good work! I’m watching all the casts.
I think the better way to transport datas through the application is with Zend_Registry.
In ZF 1.10 when we run the command zf create project SessionEx, we will already get an public function init() in the IndexController. We can write all the code in preDispatch() function in here and it works well.
I also changed the init() to preDispatch() and it never complained.
So is init() the new preDispatch() which is kept for backward compatibility or is there any difference?
@ Christy
this is a brilliant comparison between the two:
Note: Usage of init() vs. preDispatch()
In the previous section, we introduced the init() method, and in this section, the preDispatch() method. What is the difference between them, and what actions would you take in each?
The init() method is primarily intended for extending the constructor. Typically, your constructor should simply set object state, and not perform much logic. This might include initializing resources used in the controller (such as models, configuration objects, etc.), or assigning values retrieved from the front controller, bootstrap, or a registry.
The preDispatch() method can also be used to set object or environmental (e.g., view, action helper, etc.) state, but its primary purpose is to make decisions about whether or not the requested action should be dispatched. If not, you should then _forward() to another action, or throw an exception.
Note: _forward() actually will not work correctly when executed from init(), which is a formalization of the intentions of the two methods.
link: http://framework.zend.com/manual/en/zend.controller.action.html
so logically for zf 1.10 the code from the preDispatch() function should go in the init() function
What if I want to pass session to a another Controller,
Do I have to create
public function preDispatch(){ … } in each Controller or
Can I do that in other way, in global file, and where ??
Thank you.
Hi
Very nice video and nicely explained in video and in comments as well. I would like to see all the videos you posted on Zend. Could you please explain the differnce between Zend_Session and Zend_Session_Namespace and how to use globally rather than in one controller.
Thanks in advance
Sonam Ahuja