Note that there are some explanatory texts on larger screens.

plurals
  1. POZend Framework 2 How to disable event listeners in multiple MVC modules
    primarykey
    data
    text
    <p>I have two MVC modules I am trying to build, one (FBWsrv) with JSON output and different security process, and the main MVC module for normal web use (FBWeb). I have the routes all setup and mostly working. The problem I am running into is they both have onBootstrap methods that attach their own event listeners and I would like to disable listeners if not in the correct MVC module.</p> <p>My routes look like so:</p> <p>Module FBWeb, the "app" module in url: </p> <pre><code>/app/controller/action/par1/val1 </code></pre> <p>Module FBWsrv, which is "wsrv" in url: </p> <pre><code>/wsrv/controller/action/par1/val1 </code></pre> <p>As I mentioned, the route are working fine. I get to the correct controller and action. But the problem is the MVC event listeners are running on both modules, when I am not even running /app from the /wsrv route. What I want to do is disable the events in /app if I am in /wsrv.</p> <p>The short version of the modules are setup like so:</p> <p>FBWeb/Module.php:</p> <pre><code>class Module { public function onBootstrap(MvcEvent $e) { $this-&gt;sm = $application-&gt;getServiceManager(); $this-&gt;config = $e-&gt;getApplication()-&gt;getConfig(); $eventManager = $e-&gt;getApplication()-&gt;getEventManager(); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener-&gt;attach($eventManager); $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'addInstanceObjects'), 5000 ); $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'securityDispatch'), 0 ); $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 100 ); $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'postDispatch'), -100 ); $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -5000); } </code></pre> <p>FBWsrv/Module.php:</p> <pre><code>class Module { public function onBootstrap(MvcEvent $e) { $this-&gt;sm = $application-&gt;getServiceManager(); $this-&gt;config = $e-&gt;getApplication()-&gt;getConfig(); $eventManager = $e-&gt;getApplication()-&gt;getEventManager(); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener-&gt;attach($eventManager); $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'addWsrvInstanceObjects'), 5900 ); //$eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'securityWsrvDispatch'), 5800 ); $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'preWsrvDispatch'), 5500 ); $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'postWsrvDispatch'), 5400 ); $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onWsrvDispatchError'), 5100); } </code></pre> <p>I have the securityWsrvDispatch disabled in FBWsrv as I test things, but I want it running its own process later. For now, I just need to detach the FBWeb events if I am running in FBWsrv as they have no relevance. I know they are all running because errors show up in the opposite module. Not sure how to do this. </p> <p>Thanks for any help!</p> <p>Greg</p> <p>EDIT/NOTES: I also tried adding a simple check for <strong>NAMESPACE</strong> before attaching the listeners, but it seems that both namespaces are always called, which creates the listeners for both modules, even when only one is desired. How do you isolate them? I tried this, which doesn't work:</p> <pre><code>if(__NAMESPACE__ == 'FBWeb'){ $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'addInstanceObjects'), 5000 ); ... </code></pre> <p>A <code>var_dump(__NAMESPACE__);</code> always shows both namespaces printed.</p> <p><strong>EDIT - A bit of a workaround</strong></p> <p>First, thanks Andrew, you sparked some idea's I am going to work into the project. But, while I am learning, I solved my primary problem by checking the route, and then simply returning from the function if it doesn't match, skipping any additional setup in the module. </p> <p>I also consolidated my dispatch events, which lessened my confusion. There really wasn't any need for the extra mess. I know this isn't the <em>right way</em> , but it did solve my problem.</p> <p>(you must have routes setup named 'fbweb' and 'fbwsrv' to match your modules, of course)</p> <p>In FBWeb/Module.php:</p> <pre><code>// in FBWeb/Module.php // (default web MVC module) public function onBootstrap(MvcEvent $e) { // ... get eventmanager ... $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 1000 ); $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -5000); } public function preDispatch(MvcEvent $e) { $this-&gt;route = $e-&gt;getRouteMatch(); $this-&gt;matchedRouteName = explode('/', $this-&gt;route-&gt;getMatchedRouteName()); $this-&gt;route_root = $this-&gt;matchedRouteName[0]; // then just test if not in correct route if($this-&gt;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) // ... } </code></pre> <p>In my other module: FBWsrv/Module.php:</p> <pre><code>// in FBWsrv/Module.php public function onBootstrap(MvcEvent $e) { // ... get eventmanager ... $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 5500 ); $eventManager-&gt;attach( \Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -4900); } public function preDispatch(MvcEvent $e) { $this-&gt;route = $e-&gt;getRouteMatch(); $this-&gt;matchedRouteName = explode('/', $this-&gt;route-&gt;getMatchedRouteName()); $this-&gt;route_root = $this-&gt;matchedRouteName[0]; // then just test if not in correct route if($this-&gt;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) // ... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload