2010年7月12日星期一

Re: [fw-mvc] Dispatch problem - between action method call and view rendering

Thanks, Matthew !

Cristian
> -- Cristian Bichis<cristi@imagis.ro> wrote
> (on Monday, 12 July 2010, 12:30 PM +0300):
>> I need to accomplish this: once an Action method is called (for all actions,
>> from any module and controller; not just for one case) so i can run my own
>> code, but BEFORE the action view is actually rendered.
>>
>> Sample of problem: I am trying to dynamically set the doctype for any action,
>> based on the layout selected by each action. So the action view is also
>> rendered with the same doctype as the layout!
> Register an action helper that defines a postDispatch() hook... but make
> sure it happens _before_ the ViewRenderer is registered with the helper
> broker.
>
> As an example, using a resource method in your bootstrap:
>
> class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
> {
> protected function _initMyhelper()
> {
> Zend_Controller_Action_HelperBroker::addHelper(
> new My_Helper_PostDispatchActions()
> );
> }
> }
>
> If you move this to a resource plugin, just make sure your configuration
> defines it _prior_ to the view plugin:
>
> resources.myhelper[] =
> resources.view[] =
>
> This will ensure that your helper gets registered first. Since the
> HelperBroker acts on helpers as a stack, i.e. FIFO, this will ensure
> that it executes before the ViewRenderer (which is registered during the
> view resource, or even later, curing dispatch() of the front
> controller).
>
> Now, as to your action helper, it would look something like the
> following:
>
> class My_Helper_PostDispatchActions
> extends Zend_Controller_Action_Helper_Abstract
> {
> public function postDispatch()
> {
> $layout = Zend_Layout::getMvcInstance();
> $layoutScript = $layout->getLayout();
>
> switch ($layoutScript) {
> case '...':
> // ...
> break;
> default:
> break;
> }
> }
> }
>
>> If checking for the current layout occurs on a controller plugin postDispatch
>> the view is already rendered so only the layout would have been rendered with
>> correct doctype, and the action view is rendered with the default doctype
>> rather than with the doctype of selected layout... So i need to actually set
>> the doctype between action method call and action view rendering...
>>
>> That's just a sample of problem. I couldn't accomplish this by using controller
>> plugin postDispatch and also with a postDispatch of a registered helper (using
>> Zend_Controller_Action_HelperBroker::addHelper);
>>
>> Cristian

没有评论: