简体   繁体   中英

How to get module name using shardManager in Laminas?

I need to get the module name of dispatched request within Module.php onBootstrap method in a zf3 application. With the previous version (which was under Zend namespace) I was able to do that by using shared event manager as mentioned below.

public function onBootstrap(MvcEvent $e)
{
    $app = $e->getApplication();
    $eventManager = $app->getEventManager();
    $sharedEventManager = $eventManager->getSharedManager();
    $sharedEventManager->attach(AbstractActionController::class, MvcEvent::EVENT_DISPATCH, function($e) {
        $controller = $e->getTarget();
        $controllerClass = get_class($controller);
        $moduleName = strtolower(substr($controllerClass, 0, strpos($controllerClass, '\\')));
        
        // rest of code
        // ..................
        // ..................

    }, 100);
}

But with the newer version of the framework (which is under Laminas namespace), it's not working. It's even not listening to the MvcEvent::EVENT_DISPATCH of the AbstractActionController::class target class. And I could found that they have removed the SharedEventManager functionality according to below link.

https://docs.laminas.dev/laminas-eventmanager/migration/removed/#sharedeventmanagerawareinterface

So the question is how to get the module name of the dispatched request within the onBootstrap method in new version of the framework? Is there an any workaround to get module name?

I don't know if this will help but to get the controller and module in a listener in Laminas I use.

In my module.php bootstrap method:

$eventManager = $event->getApplication()->getEventManager();
$eventManager->attach(\Laminas\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'), -100);

and in the onDispatch method:

public function onDispatch(\Laminas\EventManager\EventInterface $event)
{
    $routeMatch = $event->getRouteMatch();
    $controller = get_class($event->getTarget());
    list($module) = explode("\\", $controller);
}

I hope this points you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM