How to do this without an actionstack
I found this code here while trying to understand what an actionstack does and why they're supposed to be so bad. I thought that an actionstack is just one type of action helper (just like flashmessenger or redirector or ViewRenderer).
But anyway, does anyone understand what this code does and how to do the same thing without an actionstack?
class MyController_Action extends Zend_Controller_Action {
function init() {
/** you might not want to add to the stack if it's a XmlHttpRequest */
if(!$this->getRequest()->isXmlHttpRequest()) {
$this->_helper->actionStack('left', 'somecontroller', 'somemodule');
$this->_helper->actionStack('center', 'somecontroller', 'somemodule');
$this->_helper->actionStack('right', 'somecontroller', 'somemodule');
}
}
class MyController extends MyController_Action {
function indexAction() {
// do something
}
}
class SomecontrollerController extends MyController_Action {
function leftAction() {
// do something
$this->_helper->viewRenderer->setResponseSegment('left_container');
}
function centerAction() {
// do something
$this->_helper->viewRenderer->setResponseSegment('center_开发者_开发问答container');
}
function rightAction() {
// do something
$this->_helper->viewRenderer->setResponseSegment('right_container');
}
}
I'd implement the actions as re-usalbe widgets (action helpers with preDispatch()
methods), as described here:
Using Action Helpers To Implement Re-Usable Widgets - phly, boy, phly
The widgets may render the output into placeholder
view helpers, and so be used anywhere in the layout
They may also render the content into other placeholder
, e.g. sidebar, like described in the manual:
protected function _initSidebar()
{
$this->bootstrap('View');
$view = $this->getResource('View');
$view->placeholder('sidebar')
// "prefix" -> markup to emit once before all items in collection
->setPrefix("<div class=\"sidebar\">\n <div class=\"block\">\n")
// "separator" -> markup to emit between items in a collection
->setSeparator("</div>\n <div class=\"block\">\n")
// "postfix" -> markup to emit once after all items in a collection
->setPostfix("</div>\n</div>");
}
The other solution is to use view helpers, which access the model data, and run them in the layout.
精彩评论