Model view controller Zend Framework 2如何在多个MVC模块中禁用事件侦听器

Model view controller Zend Framework 2如何在多个MVC模块中禁用事件侦听器,model-view-controller,module,zend-framework2,Model View Controller,Module,Zend Framework2,我正在尝试构建两个MVC模块,一个是带有JSON输出和不同安全过程的(FBWsrv),另一个是用于正常web使用的主MVC模块(FBWeb)。我有所有的路线设置和大部分工作。我遇到的问题是,它们都有onBootstrap方法来连接它们自己的事件侦听器,如果不在正确的MVC模块中,我想禁用侦听器 我的路线是这样的: 模块FBWeb,url中的“应用程序”模块: /app/controller/action/par1/val1 模块FBWsrv,在url中为“wsrv”: 正如我提到的,这条路线运

我正在尝试构建两个MVC模块,一个是带有JSON输出和不同安全过程的(FBWsrv),另一个是用于正常web使用的主MVC模块(FBWeb)。我有所有的路线设置和大部分工作。我遇到的问题是,它们都有onBootstrap方法来连接它们自己的事件侦听器,如果不在正确的MVC模块中,我想禁用侦听器

我的路线是这样的:

模块FBWeb,url中的“应用程序”模块:

/app/controller/action/par1/val1
模块FBWsrv,在url中为“wsrv”:

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

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

FBWeb/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, '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);

    }
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);

    }
// 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)
    // ...

}
// 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)
    // ...

}
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, '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);

    }
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);

    }
// 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)
    // ...

}
// 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)
    // ...

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

谢谢你的帮助

格雷格

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

if(__NAMESPACE__ == 'FBWeb'){
            $eventManager->attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'addInstanceObjects'), 5000 );
         ...
一个
var\u转储(名称空间)始终显示打印的两个名称空间

编辑-有点变通方法

首先,谢谢安德鲁,你激发了一些想法,我将参与这个项目。但是,在学习的过程中,我通过检查路径解决了主要问题,如果路径不匹配,只需从函数返回,跳过模块中的任何附加设置

我还整合了我的调度事件,这减少了我的困惑。真的没有必要再弄得一团糟了。我知道这不是正确的方法,但它确实解决了我的问题

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

在FBWeb/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, '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);

    }
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);

    }
// 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)
    // ...

}
// 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)
    // ...

}
在我的另一个模块中: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, '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);

    }
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);

    }
// 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)
    // ...

}
// 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)
    // ...

}

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

/**
 * 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);
}

好主意,安德鲁,谢谢!我确实遇到了埃文·库里的一篇文章,这篇文章看起来正是你所描述的。我想这会满足我的部分需求。但对于其中的一些,我必须考虑一下。之前读过他的帖子让我很困惑但我认为你的提及只是引发了一个想法,所以我开始测试…:)但与此同时,我可能有一个变通方法,我从Rob Allen那里找到了:我将在上面发布注释。谢谢好的,我知道这是处理这个问题的正确方法。不过,我不确定我的ACL在关闭时将如何工作。我需要修改一些preDispatch设置,使其与getSharedManager()配合使用,以便现有代码暂时无法工作。将此标记为解决方案。谢谢你抽出时间来帮助我,安德鲁!当然,您不需要使用闭包,用数组($object,'method')或'class::method'等替换闭包:)