Zend controller preprocessing method

I read this to understand the Zend MVC request life cycle. But I can’t think of any cases in zend where I would use the controller preset method, is the init method not enough for the code that I want to execute before the controller’s actions.

What should definitely be in the controller pre-configuration, not init.

Can you give an example?

+4
source share
3 answers

See Zend_Controller_Action - Initializing Objects and the next section, Pre and Post Dispatch Hooks. Both of them are well versed in two and Action Controller itself.

init() more suitable for setting up a controller object and performing initialization, which will be available for all your actions. Since init() works before preDispatch() , everything you configured in init() will be available to preDispatch() . Although you can redirect or redirect with init() , it is best to do this with preDispatch() since it runs before the controller action is dispatched.

From the manual:

Note. Using init () and preDispatch () What is the difference between them (init and preDispatch) and what actions do you take in each?

The init () method is intended primarily for extending the constructor. Typically, your constructor should just set the object state, and not execute a lot of logic. This may include initializing the resources used in the controller (for example, models, configuration objects, etc.) or assigning values ​​received from the front of the controller, bootstrap or registry.

The preDispatch () method can also be used to set the object, or (for example, a view, action helper, etc.), but its primary purpose is to decide whether the requested action should really be sent. If not, then you should _forward () another action or exception.

Note. _forward () will not actually work correctly when executed from init () , which is a formalization of the intentions of the two methods.

+9
source

to extend drew010's answer, here is an example of how I use preDispatch () and int ():

 public function preDispatch() { $this->_helper->layout->setLayout('admin'); } /** *initiaize the flashmessenger and assign the _session property */ public function init() { if ($this->_helper->FlashMessenger->hasMessages()) { $this->view->messages = $this->_helper->FlashMessenger->getMessages(); } //set the session namespace to property for easier access $this->_session = new Zend_Session_Namespace('location'); } 

I use preDispatch () to set the layout for each action, since it is not the default layout, and in init () I initialize my flash messenger and set up the session namespace for this controller and initialized the session as a property.

+2
source

Here is one popular piece of information in which you can spend tons of resources using init () instead of preDispatch (): if you perform access control using the preDispatch () method of the controller plugin, then the sequence of calls will be: YourController :: init (), YourAccessPlugin :: preDispatch (), YourController :: preDispatch (), YourController :: whateverAction. This means that if you do the hard work in init (), then unauthorized users can initiate it . Say, for example, you are launching a new session namespace in init (), then pointless search bots can cause your session database to be cluttered with empty sessions. Therefore, stick to simple simple things in init, avoid touching or modifying any resources, and avoid accessing the database.

+1
source

Source: https://habr.com/ru/post/1396966/


All Articles