Problems with events in Symfony2
天蓬老师
天蓬老师 2017-05-16 16:44:46
0
1
547

Today I looked at the event chapter in symfony and did an experiment myself, but there is one thing I don’t quite understand.
I first defined an Eents enumeration class to manage all events

<?php
namespace Wolehao\HomeBundle\Event;


final class Events{
  const HOMEPAGE_VISIT = "home.homepage_visit";
}

An event is defined in HomepageBundle

<?php
namespace Wolehao\HomeBundle\Event;
// 这个是sf为你提供的一个基础类
use Symfony\Component\EventDispatcher\Event;

// 你的事件类
class HomepageVisit extends Event {
  public $container;
  public function __construct($container) {
    $this->container = $container;
  }
}

Then define a listener in FileBundle


<?php namespace Wolehao\FileBundle\EventListener; use Wolehao\HomeBundle\Event\HomepageVisit; class FileListener { public function onHomepageVisit(HomepageVisit $event) { $event->container->get("logger")->info("我执行了"); // ... } }

Register service in service.xml

    <services>
        <service id="file.listener" class="Wolehao\FileBundle\EventListener\FileListener">
                <tag name="kernel.event_listener" event="home.homepage_visit" method="onHomepageVisit"/>
        </service>
    </services>

Finally I triggered the event in the controller of HomepageBundle

<?php

namespace Wolehao\HomeBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Wolehao\HomeBundle\Event\HomepageVisit;
use Wolehao\HomeBundle\Event\Events;
use Wolehao\FileBundle\Event\Listener;

class DefaultController extends Controller
{
    /**
     * @Route("/home/{name}")
     * @Template()
     */
    public function indexAction($name)
    {

      $event = new HomepageVisit($this->container);
      // 在controller里取事件分发器
      $dispatcher = $this->get('event_dispatcher');
      $dispatcher->dispatch(Events::HOMEPAGE_VISIT, $event);
      return array('name' => $name);
    }

}

First I access through http://localhost/fm/web/app_dev.php/home/index
Can correctly print out the "I executed" log
Everything seems to be OK, indicating that the event was successfully triggered and executed

Now by clicking the debug bar, the event item is as shown below:

What I don’t understand is why home.homepage_visit is in Not Called Listeners, but it is obviously executed?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(1)
曾经蜡笔没有小新

What is your specific version of symfony 2?

If you do write a log, it means that your event-related code is valid.

The information displayed in the profiler (debug bar) is collected through the data collector. As for why the call to the listener is not recorded, you need to know more details to understand. It is recommended that you check the correspondence between the xdebug token and the profiler information in detail.

In addition, do not directly inject the container into the event, as this dependency is too broad.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template