ZF3 - EventManager 和调度事件

ZF3 - EventManager and dispatch event

在较旧的 ZF2 应用程序中,如果匹配的路由以 admin 开头,我会更改调度侦听器中的布局。现在我开始了一个新项目,想使用 ZF3 组件,但是事件管理器确实有一些变化,我得到以下异常:

Uncaught TypeError: Argument 2 passed to Zend\EventManager\EventManager::attach() must be callable, none given

我真的不知道如何在 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

我希望因为你没有指定事件名称所以你收到了错误消息

更新:

请参阅 link 事件管理器从 v2 到 v3 的迁移指南

Removed functions

在 ZF3 中,您可以通过这种简单的方式更改控制器的布局:

<?php
namespace YourCompanyModule;

use Zend\ModuleManager\ModuleManager;
use Zend\Mvc\MvcEvent;

class Module
{
    // The "init" method is called on application start-up and 
    // allows to register an event listener.
    public function init(ModuleManager $manager)
    {
        // Get event manager.
        $eventManager = $manager->getEventManager();
        $sharedEventManager = $eventManager->getSharedManager();
        // Register the event listener method. 
        $sharedEventManager->attach(__NAMESPACE__, 'dispatch', 
                                    [$this, 'onDispatch'], 100);
    }

    // Event listener method.
    public function onDispatch(MvcEvent $event)
    {
        // Get controller to which the HTTP request was dispatched.
        $controller = $event->getTarget();
        // Get fully qualified class name of the controller.
        $controllerClass = get_class($controller);
        // Get module name of the controller.
        $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\'));

        // Switch layout only for controllers belonging to our module.
        if ($moduleNamespace == __NAMESPACE__) {
            $viewModel = $event->getViewModel();
            $viewModel->setTemplate('layout/layout2');  
        }        
    }

    // ...
}