Fix exceptions created in Zend Framework controller plugins

I have an Acl plugin that extends Zend_Controller_Plugin_Abstract, this plugin processes all my Acl code.

I want to throw an exception in this plugin, for example. a Exception_Unauthorised, and then handle this in mine ErrorController, so I can use the same Acl plugin for different applications and use it ErrorControllerto handle each situation in each application differently - if necessary.

The problem is that throwing an Exception from the plugin does not stop the execution of the original Action. This way I get the original Action output and output ErrorController.

How can I get the exception thrown in the plugin to stop the original action?

Case 1

// This throws the `Exception_NoPermissions`, but it does not get caught by
// `ErrorController`
public function preDispatch(Zend_Controller_Request_Abstract $request)
{       
    parent::preDispatch($request);
    throw new Exception_NoPermissions("incorrect permissions");
}

Case 2

// This behaves as expected and allows me to catch my Exception
public function preDispatch(Zend_Controller_Request_Abstract $request)
{       
    parent::preDispatch($request);
    try
    {
        throw new Exception_NoPermissions("incorrect permissions");
    }
    catch(Exception_NoPermissions $e)
    {

    }
}

3

, , .

public function preDispatch(Zend_Controller_Request_Abstract $request)
{       
    parent::preDispatch($request);

    // Attempt to log in the user

    // Check user against ACL

    if(!$loggedIn || !$access)
    {
        // set controller to login, doing this displays the ErrorController output and
        // the login controller
        $request->getControllerName("login");
    }
}
+5
4

IRC- #zftalk, / , , .

, , , . ErrorHandler, , .

, ErrorHandler routeShutdown, . . , , preDispatch, .

, , , .

+4

, .

// Get Request Object...
$request = $this->getRequest();
// Do manual redirect.. select your own action...
$this->getRequest()->setControllerName('error')->setActionName('could-not-find-destination')->setDispatched(true);
$error = new Zend_Controller_Plugin_ErrorHandler();
$error->type = Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER;
$error->request = clone( $request );
$error->exception = $e; // If you have caught the exception to $e, set it. 
$request->setParam('error_handler', $error);
0

All Articles