How can I populate Zend_Layout variables with backend data?

When trying to adhere to established best practices, for example, to avoid single games, the registry, static properties, and base controllers, how can I populate my layout (and the partial ones used by the layout) with data that is used only by the layout and is common in all actions?

A typical scenario is a menu that is built on variable data, such as a database. Keeping a separation of concerns, views / layout should never directly access the backend, but rather communicate what to contain.

Using the front controller plugin is simply not possible without using a singleton function in Zend_Layout. The plugin knows only the request and response object, does not have access to controllers, views or layout.

Zend action assistants have init / preDispatch / postDispatch methods. You can add action assistants to HelperBroker (for example, using bootstrap), and they will be executed in a normal application thread.

Using the init method to enter data into the view is not possible, since it starts before the controller / view is ready. preDispatch / postDispatch is possible, but not perfect, since these methods always fire when a controller action is executed.

This means that all use of Zend_Controller_Action :: _ forward () will also do preDispatch / postDispatch in all action assistants. This doesn't really matter for the code, except for speed, I really don't want to set the view variable (or the view helper) several times. You can create code around this problem using some $ firstRun variable, but I really don't want to keep track of this in my own code.

Another method does this in bootstrap, but, in my opinion, it really does not belong.

So, how can I populate my layout / view helper with data from the database, doing this only once and still maintaining a good separation of problems?

+5
source share
3 answers

ActionHelper, preDispatch. , . , Zend_Controller_Action::_forward(), , .

, ,

0

Action Helpers, answer ( , )

0

. , PHP/partials/render/viewHelper ( ).

preDispatch

if (!($this->view->myVar)) { // or array_key_exist or isset - depending on your use case
    $this->view->myVar = $someModel->getSomeData();
}

.

0

All Articles