应用程序模块中未触发事件

问题描述 投票:0回答:1

我似乎无法启动我的活动。这是我第一次和他们一起玩。如果你看的话,我会在

init
方法中创建一个事件,然后尝试在
onBootstrap
方法中触发它。该事件应该产生带有字符串的骰子,但事实并非如此。任何帮助将不胜感激。

namespace Application;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ControllerProviderInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\InitProviderInterface;
use Zend\ModuleManager\Feature\ViewHelperProviderInterface;
use Zend\ModuleManager\ModuleManagerInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\EventManager\EventInterface;
use Doctrine\DBAL\DriverManager;

class Module implements 
    AutoloaderProviderInterface,
    ControllerProviderInterface,
    BootstrapListenerInterface,
    ServiceProviderInterface,
    ConfigProviderInterface,
    InitProviderInterface,
    ViewHelperProviderInterface
{
    public function init(ModuleManagerInterface $manager)
    {
        $eventManager = $manager->getEventManager();
        $eventManager->attach('do',function($e){
            $event = $e->getName();
            $target = get_class($e->getTarget());
            $params = $e->getParams();
            $str = sprintf(
                'Handled event \"%s\" on target \"%s\", with parameters %s',
                $event,
                $target,
                json_encode($params)
            );
            die($str);
        });
    }

    public function onBootstrap(EventInterface $e)
    {
        $this->attachEventManagerToModuleRouteListener($e);        
        $this->setupDoctrineConnectionMappings($e);
        $eventManager = $e->getApplication()->getEventManager();
        $eventManager->trigger('do',$this,array('j','o','n'));
    }

    public function getConfig()
    {
        return array_merge( 
            include __DIR__ . '/config/module.config.php',
            include __DIR__ . '/config/routes.config.php'            
        );
    }

    public function getServiceConfig()
    {
        return include __DIR__ . '/config/services.config.php';
    }

    public function getControllerConfig()
    {
        return include __DIR__ . '/config/controllers.config.php';
    }

    public function getViewHelperConfig()
    {
        return include __DIR__ . '/config/view.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__                    
                ),
            ),
        );
    }

    private function attachEventManagerToModuleRouteListener(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }

    private function setupDoctrineConnectionMappings(MvcEvent $e)
    {
        $driver = $e->getApplication()->getServiceManager()->get('doctrine.connection.orm_default');
        $platform = $driver->getDatabasePlatform();
        $platform->registerDoctrineTypeMapping('enum', 'string');
        $platform->registerDoctrineTypeMapping('set', 'string');
    }
}
php events zend-framework2 zend-framework-mvc
1个回答
1
投票

您从

EventManager
获得的
ModuleManager
与您触发事件
EventManager
Application
EventManager
不同。

由于在模块初始化期间应用程序尚不可用,因此您必须通过

do

将侦听器绑定到事件。


所以附加到应用程序的事件

SharedManager

会像这样


do

请注意,共享管理器需要预期触发事件的 EventManager 的标识符,在本例中是(通常是)
$sharedManager = $manager->getEventManager()->getSharedManager(); $sharedManager->attach(Application::class, 'do', function($e) { // event code });

的类名。

    

© www.soinside.com 2019 - 2024. All rights reserved.