0

我正在关注一本书,它展示了如何触发一个新事件并开始监听它。该事件称为通道 25,它仅包含用于测试的虚构数据。我将 error_log 用于捕获事件过程,以便我知道它的工作原理。该事件确实被触发,但由于某种原因我无法听它。这是module.php的副本

我已经评论了我触发事件的地方以及我在哪里听它的部分。

<?php

namespace Debug;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\ModuleManager\ModuleManager;
use Zend\eventManager\Event;
use Zend\Mvc\MvcEvent;
use Zend\EventManager\EventManager; //manage events (create/listen for events)

class Module implements AutoloaderProviderInterface
{

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

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


    public function init(ModuleManager $ModuleManager)
    {
        $eventManager = $ModuleManager->getEventManager();
        $eventManager->attach('loadModules.post', array($this, 'loadedModulesInfo'));

        //create new event
        $event = new EventManager('channel-25');
        $event->trigger('new song', null, array('artist' => 'Adele'));
        error_log('New Event Triggered');
    }

    public function loadedModulesInfo(Event $event)
    {
        $moduleManager = $event->getTarget();
        $loadedModules = $moduleManager->getLoadedModules();
        error_log(var_export($loadedModules, true));
    }

    public function onBootstrap(MvcEvent $event)
    {
        //Now i will be listening for my sample event
        $eventManager = $event->getApplication()->getEventManager();
        $sharedEventManager = $eventManager->getSharedManager();
        $sharedEventManager->attach('channel-25', 'new song', function(Event $event) {
            $artist = $event->getParam('artist');
            error_log('Found the Event. The artist is: ' . $artist);
        });
    }

    public function handleError(MvcEvent $event)
    {
        $controller = $event->getController();
        $error = $event->getParam('error');
        $exception = $event->getParam('exception');
        $message = 'Error: ' . $error;

        if($exception instanceof \Exception)
        {
            $message .= ', Exception(' . $exception->getMessage() . '):' . $exception->getTraceAsString();
        }

        error_log($message);
    }


}
4

2 回答 2

0

我想到了。我将事件触发器移动到索引控制器并且它起作用了。对于其他试图弄清楚事件的人来说,这就是我所做的:

索引控制器.php

namespace Debug\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Mvc\MvcEvent;
use Zend\EventManager\EventManager;


class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        //create new event
        $event = new EventManager('channel-25');
        $event->trigger('new song', null, array('artist' => 'Adele'));
        error_log('New Event Triggered');
    }
}

模块.php

<?php

namespace Debug;

use Zend\ModuleManager\ModuleManager;
use Zend\eventManager\Event;
use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $event)
    {
        //Now i will be listening for my sample event
        $eventManager = $event->getApplication()->getEventManager();
        $sharedEventManager = $eventManager->getSharedManager();
        $sharedEventManager->attach('channel-25', 'new song', function(Event $event) {
            $artist = $event->getParam('artist');
            error_log('got the new song: ' . $artist);
        });
    }


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

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

}
于 2013-10-31T02:30:20.173 回答
0

我可以看到您自己找到了解决方案。代码不起作用的原因是您在开始侦听之前触发了事件。在您的第二篇文章中,事件的触发是在已经有一个监听器之后完成的。哪个是正确的顺序。

--

PS:如果有人有兴趣学习 Zend Framework 2,你可以试试http://LearnZF2.com

于 2013-10-31T09:42:30.853 回答