Php 无法在模块init()方法中附加事件
我阅读了一些关于ZF2的最佳实践。在这里,解释了在模块的模块类的init()方法中附加来自MVC的事件:Php 无法在模块init()方法中附加事件,php,zend-framework2,Php,Zend Framework2,我阅读了一些关于ZF2的最佳实践。在这里,解释了在模块的模块类的init()方法中附加来自MVC的事件: class Module { public function getAutoloaderConfig() { return array( 'Zend\Loader\ClassMapAutoloader' => array( __DIR__ . '/autoload_classmap.php', ), )
class Module {
public function getAutoloaderConfig() {
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
);
}
public function init(ModuleManager $moduleManager) {
echo 'init<br>';
$em = $moduleManager->getEventManager();
$em->attach(MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
$em->attach(MvcEvent::EVENT_ROUTE, array($this, 'onRoute'));
}
public function onDispatch(MvcEvent $e){
echo 'onDispatch<br>';
}
...
类模块{
公共函数getAutoloaderConfig(){
返回数组(
'Zend\Loader\ClassMapAutoloader'=>数组(
__DIR\uuu.'/autoload\u classmap.php',
),
);
}
公共函数初始化(ModuleManager$ModuleManager){
回声“init
”;
$em=$moduleManager->getEventManager();
$em->attach(MvcEvent::EVENT_DISPATCH,数组($this,'onDispatch'));
$em->attach(MvcEvent::事件路由,数组($this,'onRoute'));
}
发布的公共功能(MvcEvent$e){
回声“onDispatch
”;
}
...
结果是没有错误,很好。但是事件没有被捕获
有什么想法吗?我也试过SharedManager,但它只适用于活动调度…参见
仅找到此解决方案:在Module.php类的onBootstrap()方法中附加侦听器:
bootstrap上的公共函数(MvcEvent$e){
echo“onBootstrap
”;
$em=$e->getApplication()->getEventManager();
$em->attach(MvcEvent::EVENT_DISPATCH,数组($this,'onDispatch'));
$em->attach(MvcEvent::事件路由,数组($this,'onRoute'));
}
…使用共享管理器会更好。下面的示例是当我们得到xmlHttpRequest时禁用布局,优先级-95是使事情正常工作的关键点
public function onBootstrap(MvcEvent $e) {
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
// Hybrid view for ajax calls (disable layout for xmlHttpRequests)
$eventManager->getSharedManager()->attach('Zend\Mvc\Controller\AbstractController', MvcEvent::EVENT_DISPATCH, function(MvcEvent $event){
/**
* @var Request $request
*/
$request = $event->getRequest();
$viewModel = $event->getResult();
if($request->isXmlHttpRequest()) {
$viewModel->setTerminal(true);
}
return $viewModel;
}, -95);
}
除非针对特定情况,否则最好在
onBootstrap
中注册事件
init
用于“早期事件”
我发现了一个非常清晰的链接:
您可以在Zend\ModuleManager\Listener\DefaultListenerAggregate::attch
中找到默认MVC事件的顺序:
public function attach(EventManagerInterface $events)
{
$options = $this->getOptions();
$configListener = $this->getConfigListener();
$locatorRegistrationListener = new LocatorRegistrationListener($options);
// High priority, we assume module autoloading (for FooNamespace\Module classes) should be available before anything else
$this->listeners[] = $events->attach(new ModuleLoaderListener($options));
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE_RESOLVE, new ModuleResolverListener);
// High priority, because most other loadModule listeners will assume the module's classes are available via autoloading
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new AutoloaderListener($options), 9000);
if ($options->getCheckDependencies()) {
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new ModuleDependencyCheckerListener, 8000);
}
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new InitTrigger($options));
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new OnBootstrapListener($options));
$this->listeners[] = $events->attach($locatorRegistrationListener);
$this->listeners[] = $events->attach($configListener);
return $this;
}
附加自
onBootstrap()
Hi,为什么它在onBootstrap中更好?如果是这种情况,为什么它在init()中不起作用?@gizmo我必须检查。我不确定这种情况是否有区别。您可以同时尝试这两种方法,看看它是否有效。它在init()中不起作用:-(但没有发生错误。它确实需要在init方法上?您可以始终使用一些die(“测试”)和除错工具进行调试。我使用xdebug的远程debuggin。它不必在init-method()中,但似乎是附加事件之类的正确位置。总之,我将它放在了onBootsrap-method()中。),即使这不是在事件发生时附加事件侦听器的最佳方式…也许我会将其移动到构造函数中
public function attach(EventManagerInterface $events)
{
$options = $this->getOptions();
$configListener = $this->getConfigListener();
$locatorRegistrationListener = new LocatorRegistrationListener($options);
// High priority, we assume module autoloading (for FooNamespace\Module classes) should be available before anything else
$this->listeners[] = $events->attach(new ModuleLoaderListener($options));
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE_RESOLVE, new ModuleResolverListener);
// High priority, because most other loadModule listeners will assume the module's classes are available via autoloading
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new AutoloaderListener($options), 9000);
if ($options->getCheckDependencies()) {
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new ModuleDependencyCheckerListener, 8000);
}
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new InitTrigger($options));
$this->listeners[] = $events->attach(ModuleEvent::EVENT_LOAD_MODULE, new OnBootstrapListener($options));
$this->listeners[] = $events->attach($locatorRegistrationListener);
$this->listeners[] = $events->attach($configListener);
return $this;
}