首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ZF3 - EventManager和调度事件

ZF3 - EventManager和调度事件
EN

Stack Overflow用户
提问于 2017-01-30 15:43:38
回答 2查看 3.6K关注 0票数 4

在较早的ZF2应用程序中,如果匹配的路由以admin开头,则更改调度侦听器中的布局。现在我启动了一个新项目,希望使用ZF3组件,但是事件管理器确实有一些更改,我得到了以下例外:

Uncaught :参数2传递给Zend\EventManager\EventManager::attach()必须是可调用的,没有给出

我不知道如何在ZF3中处理这个问题。下面是更改ZF2应用程序中布局的相关源代码:

Module.php

代码语言:javascript
复制
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

代码语言:javascript
复制
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');
        }
    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-30 17:29:03

zf3更关注于解耦组件,似乎已经删除了聚合以附加事件(请参阅api文档)。

事件管理器

简单地说,附加消息是

代码语言:javascript
复制
attach($eventName, callable $listener, $priority = 1) : callable

我希望由于您没有指定eventName,所以您将收到错误消息。

最新情况:

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

移除函数

票数 1
EN

Stack Overflow用户

发布于 2018-04-20 09:12:47

在ZF3中,您可以简单地更改控制器的布局:

代码语言:javascript
复制
<?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');  
        }        
    }

    // ...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41939905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档