2

我正在尝试构建两个 MVC 模块,一个 (FBWsrv) 具有 JSON 输出和不同的安全过程,以及用于正常 Web 使用的主 MVC 模块 (FBWeb)。我已经完成了所有路线的设置,并且大部分时间都在工作。我遇到的问题是他们都有 onBootstrap 方法来附加他们自己的事件侦听器,如果不在正确的 MVC 模块中,我想禁用侦听器。

我的路线如下所示:

模块 FBWeb,url 中的“app”模块:

/app/controller/action/par1/val1

模块 FBWsrv,即 url 中的“wsrv”:

/wsrv/controller/action/par1/val1

正如我所提到的,这条路线运行良好。我得到了正确的控制器和动作。但问题是 MVC 事件侦听器正在两个模块上运行,而我什至没有从 /wsrv 路由运行 /app。如果我在 /wsrv 中,我想要做的是禁用 /app 中的事件。

模块的简短版本设置如下:

FBWeb/模块.php:

class Module 
{

    public function onBootstrap(MvcEvent $e)
    {

        $this->sm = $application->getServiceManager();
        $this->config = $e->getApplication()->getConfig();

        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'addInstanceObjects'), 5000 );
        $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'securityDispatch'), 0 );
        $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 100 );
        $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'postDispatch'), -100 );
        $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -5000);

    }

FBWsrv/Module.php:

class Module 
{
    public function onBootstrap(MvcEvent $e)
    {

        $this->sm = $application->getServiceManager();
        $this->config = $e->getApplication()->getConfig();
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'addWsrvInstanceObjects'), 5900 );
        //$eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'securityWsrvDispatch'), 5800 );
        $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'preWsrvDispatch'), 5500 );
        $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'postWsrvDispatch'), 5400 );
        $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onWsrvDispatchError'), 5100);

    }

我在测试时在 FBWsrv 中禁用了 securityWsrvDispatch,但我希望它稍后运行自己的进程。现在,如果我在 FBWsrv 中运行,我只需要分离 FBWeb 事件,因为它们没有相关性。我知道它们都在运行,因为错误出现在相反的模块中。不知道该怎么做。

谢谢你的帮助!

格雷格

编辑/注释:我还尝试在附加侦听器之前为NAMESPACE添加一个简单的检查,但似乎总是调用两个命名空间,这会为两个模块创建侦听器,即使只需要一个。你如何隔离它们?我试过这个,它不起作用:

if(__NAMESPACE__ == 'FBWeb'){
            $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'addInstanceObjects'), 5000 );
         ...

Avar_dump(__NAMESPACE__);总是显示打印的两个命名空间。

编辑 - 一个解决方法

首先,感谢 Andrew,您激发了我将要参与该项目的一些想法。但是,在我学习的过程中,我通过检查路线解决了我的主要问题,如果不匹配则简单地从函数中返回,跳过模块中的任何其他设置。

我还合并了我的调度事件,这减少了我的困惑。真的不需要额外的混乱。我知道这不是正确的方法,但它确实解决了我的问题。

(当然,你必须有名为“fbweb”和“fbwsrv”的路由设置来匹配你的模块)

在 FBWeb/Module.php 中:

// in FBWeb/Module.php
// (default web MVC module)

public function onBootstrap(MvcEvent $e)
{
    // ... get eventmanager ...

    $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 1000 );
    $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -5000);

}

public function preDispatch(MvcEvent $e)
{
    $this->route = $e->getRouteMatch();
    $this->matchedRouteName = explode('/', $this->route->getMatchedRouteName());
    $this->route_root = $this->matchedRouteName[0];

    // then just test if not in correct route
    if($this->route_root != 'fbweb'){
        // if NOT, bail here, 
        // and skip any other setup in this module dispatch
        return;
    }

    // continue other normal stuff (ACL's, session stuff, etc)
    // ...

}

在我的另一个模块中:FBWsrv/Module.php:

// in FBWsrv/Module.php

public function onBootstrap(MvcEvent $e)
{
    // ... get eventmanager ...

    $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 5500 );
    $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -4900);

}

public function preDispatch(MvcEvent $e)
{
    $this->route = $e->getRouteMatch();
    $this->matchedRouteName = explode('/', $this->route->getMatchedRouteName());
    $this->route_root = $this->matchedRouteName[0];
    // then just test if not in correct route
    if($this->route_root != 'fbwsrv'){
        // if NOT, bail here, 
        // and skip any other setup in this module dispatch
        return;
    }

    // continue other normal stuff (ACL's, session stuff, etc)
    // ...

}
4

1 回答 1

5

为什么不通过共享事件管理器使用上下文附加事件,有很多文档可以帮助共享事件管理器。

/**
 * On bootstrap event
 * 
 * @param \Zend\Mvc\MvcEvent $e
 */
public function onBootstrap(MvcEvent $e)
{
    // .. other stuff

    /**
     * Example attaching events with shared manager, using context
     */
    $e->getApplication()->getEventManager()->getSharedManager()
        ->attach('MyModule\Controller\AbstractActionController', 'dispatch', function($e) {

        // do something to only be triggerd when dispatch is triggered
        // on a controller extending your nice base controller in this module..

    }, 100);
}
于 2013-04-23T22:08:06.553 回答