Multiple action contexts in Zend
I am new to Zend and am working on a project that requires three contexts for a particular action. There is the standard context that will normally be used, an AJAX context for AJAX calls, and finally a print-friendly context. The goal is for each of these to have their own view, so the view files used would be something like:
/action_name.phtml /action_name.ajax.phtml /action_name.print.phtml
I read http://framework.zend.com/manual/en/zend.controller.actionhelpers.html and came up with:
public function init()
{
// add any necessary context switching here
$contextSwitch = $this->_helper->getHelper('AjaxContext');
$contextSwitch->addActionContext('history', 'html')
->initContext();
//need to add another context for the print view
$this->_helper->getHelper('contextSwitch')->addActionContext('history','print')->i开发者_运维知识库nitContext();
}
The first two lines I am convinced works, but I am not sure if I am going about the print context the right way, since in the examples the second parameter is normally a file type, like JSON, XML, HTML, etc. Am I going about things the right way or is there something else I should be doing?
It's everything in the documentation. If you want custom contexts, you have to add them first:
$this->_helper
->getHelper('contextSwitch')
->addContext('print', array(
// context options go here
))
->addActionContext('history', 'print')
// more addActionContext()s goes here
->initContext();
What you might do instead of using a context for the print view is just have a parameter in the URL like /print/1
. Then in the controller action, check to see if that parameter is true, and if it is, render the "print" view script instead of the regular view script.
精彩评论