DatsunBing
DatsunBing

Reputation: 9076

ZF2: Catching an exception in the dispatch.error event

My service and model layer often throw Exception\loginRequired() and Exception\incorrectUser(). I want to catch these, and if appropriate, redirect the user to a login page, with the original URI and it's parameters appended to the GET string. Upon successful login, I will then redirect the user to the original request.

I have a function set up in my module class ready to handle dispatch exceptions, but my question is, how do I access the exception?

public function onBootstrap(MvcEvent $e)
{
    // initialise the event manager
    ...

    // catch exceptions
    $eventManager->attach('dispatch.error', function ($e) {

       // How to access the exception here?

    });
}

I need to be able to access the exception to test it's type.

I have checked out the MVC Module, the Event Manager and the Response object, and while I can find references to getError(), they return strings and not exceptions. How do I get the exception?

I have seen some questions on this topic but they are using the ZfcUser module, which I am not.

Upvotes: 0

Views: 1918

Answers (1)

DatsunBing
DatsunBing

Reputation: 9076

So, after looking at Zend\Mvc\View\Http\ExceptionStrategy I found that the exception is available as a parameter of the MvcEvent, accessible via $e->getParam('exception');

Upvotes: 3

Related Questions