Zend Framework 1.11.11
我正在编写一个Zend Framework Controller插件,它在routeShutdown
挂钩中执行操作。
如果存在路由错误,我希望能够避免运行我的进程。
即。如果我们要收到404错误,我不想运行这些流程。
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?
}
...
}
那么,我们如何确定此时是否存在路由错误?
我尝试过的事情。
检查requestObj中的模块,控制器和操作名称
检查响应对象中的异常
$this->getResponse()->isException()
似乎返回FALSE,即使出现路由错误。任何帮助表示感谢。
答案 0 :(得分:3)
我自己已经解决了。
我是愚蠢的。在routeShutdown,我们不知道它是否会成为404页面类型错误,直到我们尝试发送它。所以我们所能做的就是
因此插件无法确定是否存在“路由错误” - 所以它必须询问“isRouteShutdownToBeSkipped()”:
<?php
class MyPlugin extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown( Zend_Controller_Request_Abstract $zfRequestObj )
{
if ( $this->isRouteShutdownToBeSkipped( $zfRequestObj ) )
{
//do not do any intensive page start up stuff
return;
}
return $this->doRouteShutdownProcesses( $zfRequestObj );
}
//...
然后isRouteShutdownToBeSkipped()方法测试
所以:
<?php
class MyPlugin extends Zend_Controller_Plugin_Abstract
{
//...
protected function isRouteShutdownToBeSkipped( Zend_Controller_Request_Abstract $zfRequestObj )
{
if ( $this->getResponse()->isException() )
{
//Skip cos there was an exception already.
return TRUE;
}
if (!( $this->isDispatchable( $zfRequestObj ) ))
{
//Skip cos route is not dispatchable (i.e no valid controller found).
return TRUE;
}
if (!( $this->actionMethodExists( $zfRequestObj ) ))
{
//There is no action method on the controller class that
//resembles the requested action.
return TRUE;
}
//else else give it a go
return FALSE;
}
//...
我的isDispatchable()
方法只是委托给调度员:
<?php
class MyPlugin extends Zend_Controller_Plugin_Abstract
{
//...
protected function isDispatchable( Zend_Controller_Request_Abstract $zfRequestObj )
{
return Zend_Controller_Front::getInstance()
->getDispatcher()
->isDispatchable( $zfRequestObj );
}
...
我的actionMethodExists()
方法有点复杂。调度程序的接口有点误导(getControllerClass()
实际上没有返回类名),所以我们必须通过一些箍来获取实际的控制器类名,然后及时加载类以调用PHP的内置{ {1}}功能:
method_exists()