事件未正确触发

时间:2016-08-31 12:58:10

标签: zend-framework2 zend-framework3

这是我的代码:

class Module
{
    public function onBootstrap(MvcEvent $e)
    {

       $app = $e->getParam('application');
       $app->getEventManager()->attach('pre.loadevents',array($this,'preLoadEvents'));
       $app->getEventManager()->attach('loadevents',array($this,'loadEvents'));
       $app->getEventManager()->attach('post.loadevents',array($this,'postLoadEvents'));
       $app->getEventManager()->trigger('loadevents',null,[]);
    }       

    public function preLoadEvents(MvcEvent $e)
    {

    }

    public function postLoadEvents(MvcEvent $e)
    {

    }

    public function loadEvents(MvcEvent $e)
    {

        $app = $e->getParam('application');
        $eventsConfig = yoda::config('module/application/events/event');
        foreach($eventsConfig as $event){
            if((int)$event->active===0)continue;
            $app->getEventManager()->attach($event->name,array($this,$event->method));
        }

    }
}

使用此功能后,我得到Fatal error: Uncaught TypeError: Argument 1 passed to Application\Module::loadEvents() must be an instance of Zend\Mvc\MvcEvent, instance of Zend\EventManager\Event given我做错了什么

1 个答案:

答案 0 :(得分:0)

You should have

public function loadEvents(\Zend\EventManager\Event $e)
{
   ...
}

instead of

public function loadEvents(MvcEvent $e)
{
   ...
}

because it is not a MVC event you are getting here.