Zend Framework 将自定义事件附加到共享事件管理器
Zend Framework attach custom event to shared event manager
使用 Zend Framework,我想在我的 Application/Module 上附加一个事件,以便在每个调度事件中为每个模块调用此函数。这是我的代码:
class 模块
{
public 函数 getConfig()
{
return 包括 DIR 。 '/../config/module.config.php';
}
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$serviceManager = $application->getServiceManager();
$sessionManager = $serviceManager->get(SessionManager::class);
// Get event manager.
$eventManager = $event->getApplication()->getEventManager();
$sharedEventManager = $eventManager->getSharedManager();
// Register the event listener method onDispatch
$sharedEventManager->attach(AbstractActionController::class,
MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch'], 100);
}
public function onDispatch(MvcEvent $event)
{
// Will perform application wide ACL control based on controller,
// action and user data.
}
}
由于某些原因,我的 onDispatch 从未被调用,即使应用程序屏幕已加载。
不知道我错过了什么。据我所知,我需要使用共享事件管理器才能对整个应用程序有效。
感谢帮助。
为此(侦听 MVC 事件),您不需要共享事件管理器,而是 MVC 事件管理器。像这样更改您的代码,它将按预期工作:.
$application = $event->getApplication();
$eventManager = $application->getEventManager();
// Register the event listener method onDispatch
$eventManager->attach(MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch'], 100);
另请阅读 this great blog post 以了解有关何时使用共享事件管理器的更多详细信息。这个特殊情况也在这个博客中解释 post:
The special case of MVC events
I said earlier that we should use the shared event manager. But there is one specific case: the event manager we retrieve from the onBootstrap
method is the MVC event manager. This means that this event manager knows the events triggered by the framework. This means that if you want to add listeners to the events of the Zend\Mvc\MvcEvent
class, you can do it without using the shared event manager:
使用 Zend Framework,我想在我的 Application/Module 上附加一个事件,以便在每个调度事件中为每个模块调用此函数。这是我的代码:
class 模块 { public 函数 getConfig() { return 包括 DIR 。 '/../config/module.config.php'; }
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$serviceManager = $application->getServiceManager();
$sessionManager = $serviceManager->get(SessionManager::class);
// Get event manager.
$eventManager = $event->getApplication()->getEventManager();
$sharedEventManager = $eventManager->getSharedManager();
// Register the event listener method onDispatch
$sharedEventManager->attach(AbstractActionController::class,
MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch'], 100);
}
public function onDispatch(MvcEvent $event)
{
// Will perform application wide ACL control based on controller,
// action and user data.
}
}
由于某些原因,我的 onDispatch 从未被调用,即使应用程序屏幕已加载。
不知道我错过了什么。据我所知,我需要使用共享事件管理器才能对整个应用程序有效。
感谢帮助。
为此(侦听 MVC 事件),您不需要共享事件管理器,而是 MVC 事件管理器。像这样更改您的代码,它将按预期工作:.
$application = $event->getApplication();
$eventManager = $application->getEventManager();
// Register the event listener method onDispatch
$eventManager->attach(MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch'], 100);
另请阅读 this great blog post 以了解有关何时使用共享事件管理器的更多详细信息。这个特殊情况也在这个博客中解释 post:
The special case of MVC events
I said earlier that we should use the shared event manager. But there is one specific case: the event manager we retrieve from theonBootstrap
method is the MVC event manager. This means that this event manager knows the events triggered by the framework. This means that if you want to add listeners to the events of theZend\Mvc\MvcEvent
class, you can do it without using the shared event manager: