2011年1月18日星期二

Re: [fw-mvc] Using Action Helpers to disable the action dispatch

Matthew,

Thanks for your response. I've learned 2 things since I've posted: 1)
The ContextSwitch action helper does not hook into preDispatch() at
all (doh!), and 2) Running $this->getRequest()->setDispatched(false);
within the preDispatch() method of an action helper will cause an
infinite loop, and an eventual timeout. I'm pretty sure this has to do
with the Do..While loop within the Front Controller's dispatch()
method. I could be wrong.

The solution (for me) is to extend Zend_Controller_Action, add
protected $_cancelAction = false, and a setCancelAction($flag) method
for changing it. Then, within the controller action's dispatch()
method, modify the IF statement to read:

if ($this->getRequest()->isDispatched() && !$this->_cancelAction) {

Once these modifications were made, a simple call to
setCancelAction(true) anywhere within the controller action's or
action helper's preDispatch() or init() method will prevent the action
from being executed, along with the controller action's postDispatch()
method. However, the action helper's postDispatch() will still
execute.

Mission accomplished!

On Mon, Jan 17, 2011 at 6:42 AM, Matthew Weier O'Phinney
<matthew@zend.com> wrote:
> -- No Spam <nospamchicago@gmail.com> wrote
> (on Saturday, 15 January 2011, 12:29 AM -0800):
>> I'd like to bypass the action dispatch from within an Action Helper.
>> I'm using a customized ContextSwitch action helper to switch the
>> context of a request, and within a certain context, I don't need to
>> execute the action. I simply need for the Action Helper to execute
>> it's pre- and postDispatch functions (which set some view variables).
>>
>> I've attempted to do this via the Action Helper's preDispatch() method
>> using $this->getRequest()->setDispatched(false).
>
> Pass a boolean "true" value instead.
>
>> However, it's not actually modifying the request within the Action
>> Controller. I've tried modifying the request, then setting the Action
>> Controller's request to the modified one. Still, no dice.
>>
>> How would I go about this?
>>
>> Thanks.
>>
>
> --
> Matthew Weier O'Phinney
> Project Lead            | matthew@zend.com
> Zend Framework          | http://framework.zend.com/
> PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
>

没有评论: