Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change event handler priority in Symfony 2

I have defined 2 event listeners for "name: kernel.event_listener, event: kernel.request", but I can do what I want, I can't change the order of calling this listener. I try to use the "priority" number, but nothing helps.

This event is always calling @first:

  MyBundle.Foo.Listener.RequestListener:
    class: %MyBundle.Foo.Listener.request.class%
    arguments: [ @session, @liip_theme.active_theme, @Need.some.infos.service ]
    tags:
      - { name: kernel.event_listener, event: kernel.request, method: onRequest, priority: 100 }

and this event is never calling, because the first event use a service that depends on a info of this event, so it crashes before this event is triggered:

  MyBundle.Bar.Listener.RequestListener:
    class: %MyBundle.Bar.Listener.request.class%
    arguments: [ @session ]
    tags:
      - { name: kernel.event_listener, event: kernel.request, method: onRequest, priority: 200 }

I have changed the "priority" too, but nothing changed :(

Maybe someone have a tip for me, that I can to ensure that listener "MyBundle.Bar.Listener.RequestListener" will call before the other event?

like image 894
Volker Avatar asked Jun 30 '12 23:06

Volker


1 Answers

The problem is that the event listener class will init at Symfony start, before any event triggers.

I had to change my DI to:

  MyBundle.Foo.Listener.RequestListener:
    class: %MyBundle.Foo.Listener.request.class%
    arguments: [ @session, @liip_theme.active_theme, @service_container ]
    tags:
      - { name: kernel.event_listener, event: kernel.request, method: onRequest, priority: 0 }

I used this "@service_container" service, and call inside the event the target service "@Need.some.infos.service" and all works fine.

like image 121
Volker Avatar answered Nov 03 '22 22:11

Volker