Zend Framework 1.11.11
I am writing a Zend Framework Controller Plugin that does stuff in the routeShutdown hook.
I’d like to be able to avoid running my processes if there are routing errors.
i.e. I do not want to run the processes if we are just gonna get a 404 error.
class MyPlugin extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown( Zend_Controller_Request_Abstract $zfRequestObj )
{
if ( $this->isRoutingError() )
{
//there was a routing error do not do any intensive page start up stuff
return;
}
return $this->doRouteShutdownProcesses( $zfRequestObj );
}
protected function isRoutingError()
{
//?? So how do we get this?
}
...
}
So, how can we determine if there was a routing error at this point?
Things I have tried.
-
Check the module, controller, action name in the requestObj
- Does not work cos that is not set to Error Action yet
-
Check for exceptions in response object
- Does not work cos
$this->getResponse()->isException()seems to return FALSE, even when there was a routing error.
- Does not work cos
Any help appreciated.
I have worked it out myself.
I was being stupid. At routeShutdown we do not know whether its going to be a 404 page type error until we try to dispatch it.
So all we can do is
So the plugin cannot really determine if there is a ‘routing error’ – so it has to ask “isRouteShutdownToBeSkipped()” :
Then the isRouteShutdownToBeSkipped() method tests for
So:
My
isDispatchable()method simply delegates to the dispatcher:My
actionMethodExists()method is a little bit more complex. The dispatcher’s interface is a bit misleading (getControllerClass()does not actually return a classname), so we have to jump through some hoops to get the actual controller class name, then load the class in time for the call to PHP’s builtinmethod_exists()function: