search

Home  >  Q&A  >  body text

symfony - Can anyone tell me the difference between subscriber and listener in sf2?

Like the title, the listener is probably clear, but the subscriber is confused.

怪我咯怪我咯2755 days ago536

reply all(1)I'll reply

  • 为情所困

    为情所困2017-05-16 16:48:15

    When registering a listener (a method of a certain class), you need to specify the event.

    The subscriber specifies event processing through the getSubscribedEvents() static method, which can be understood as batch registration. The return value of getSubscribedEvents() is an array, and the key is the event name. The corresponding nested array lists the methods that need to be triggered for this event and its priority (the one with the larger value is triggered first, -1024~1024)

    class ExampleSubscriber implements EventSubscriberInterface
    {
        static public function getSubscribedEvents()
        {
            return array(
                'kernel.response' => array( // <-- 事件
                    array('onKernelResponseFirst', 5), // <-- 第一个回调,优先级5
                    array('onKernelResponseSecond', 0) // <-- 第二个回调,优先级0
                )
            );
        }
    
        public function onKernelResponseFirst(FilterResponseEvent $event)
        {
            // ...
        }
    
        public function onKernelResponseSecond(FilterResponseEvent $event)
        {
            // ...
        }
    }

    reply
    0
  • Cancelreply