触发新事件并聆听它

时间:2013-10-31 01:08:07

标签: php zend-framework2

我正在关注一本书,其中展示了如何触发新事件并开始倾听。该事件被称为channel-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);
    }


}

2 个答案:

答案 0 :(得分:0)

我明白了。我将事件触发器移动到索引控制器并且它工作正常。 对于任何试图找出事件的人来说,这就是我所做的:

indexController.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');
    }
}

Module.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';
    }

}

答案 1 :(得分:0)

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

-

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