views:

44

answers:

2

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.

+2  A: 

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.

$request->setDispatched(false)
        ->setControllerName("error")
        ->setActionName("jsonDecoding");

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.

madsleejensen
Thanks. I found I can use `setErrorHandler()` on the default ErrorHandler plugin to have exceptions handled in the same controller
mrclay
+2  A: 

I would throw an exception (and then catch it in error controller) - that's what you do when there is non-recoverable error.

Tomáš Fejfar