Zend Framework 1 - 如何在routeShutdown中检测插件中的路由错误

时间:2012-05-03 17:53:06

标签: zend-framework plugins

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中的模块,控制器和操作名称

    • 无法使用未设置为错误操作的cos
  • 检查响应对象中的异常

    • 不起作用cos $this->getResponse()->isException()似乎返回FALSE,即使出现路由错误。

任何帮助表示感谢。

1 个答案:

答案 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()方法测试

  • 一般例外或
  • undispatchable controllers
  • 不存在的操作方法。 (我避免在我的项目中使用魔法,所以我知道如果aint声明的方法,那么它就不会起作用了。)

所以:

<?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()