如果匹配的路由以admin
开头,则在较旧的ZF2应用程序中,我会在调度侦听器中更改布局。现在,我开始了一个新项目,并想使用ZF3组件,但是事件管理器确实进行了一些更改,并且出现以下异常:
未捕获的TypeError:传递给Zend \ EventManager \ EventManager :: attach()的参数2必须是可调用的,没有给出
我真的不知道如何在ZF3中处理此问题。这是在ZF2应用程序中更改布局的相关源代码:
Module.php
namespace Admin;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
class Module implements BootstrapListenerInterface {
public function onBootstrap(EventInterface $event) {
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$eventManager->attach($serviceManager->get('Admin\Listener\Dispatch'));
}
}
DispatchListener.php
namespace Admin\Listener;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\MvcEvent;
class DispatchListener extends AbstractListenerAggregate {
public function attach(EventManagerInterface $eventManager) {
$this->listeners[] = $eventManager->attach(
MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'), 100
);
}
public function onDispatch(EventInterface $event) {
$matchedRouteName = $event->getRouteMatch()->getMatchedRouteName();
if (strpos($matchedRouteName, 'admin') === 0) {
$event->getViewModel()->setTemplate('layout/admin');
}
}
}
最佳答案
zf3更加专注于去耦组件,似乎聚合已被删除以附加事件,请参见api文档
event manager
简而言之,附加消息说
attach($eventName, callable $listener, $priority = 1) : callable
我希望由于您未指定eventName而收到错误消息
更新:
请参阅事件管理器从v2到v3的迁移指南的链接
Removed functions
关于zend-framework2 - ZF3-EventManager和调度事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41939905/