During Zend_Controller_Action::init(), is there a way to cancel the action (so it won’t be called)?
<?php
class JsonApiController extends Zend_Controller_Action {
function init()
{
// try JSON decoding the raw request body
if ($jsonDecodingFailed) {
echo '{"error":"invalid JSON"}';
$this->_cancelAction(); // something like this exist?
}
}
}
My current workaround is to make an empty nullAction() method and call $this->_forward('null') to forward to it.
Theres nothing wrong in using $this->_forward() inside the init() (if the method you want to forward to is inside the same controller as the init()), this will simply change the request object’s controller / action (overwrite what has been set via the router).
An alternative to this would be to create a Zend_Controller_Plugin as it looks like you are handling unexpected errors.
Have a look at the Zend_Controller_Plugin_ErrorHandler implementation.
So instead of forwarding to another action you would throw an exception, and have your custom plugin check in postDispatch() if the response contains eny expections, and if it does simply edit the current Request object to show your “cancel” action.
The last way could be to just throw a exit after the echo “{error: “invalid json”}” not very nice but you will avoid the overload of having another dispatch iteration.