3

I am making services and want to pass 'container' as argument. because I want to use like this

$user = $this->container->get('security.context')->getToken()->getUser();

my Acme/MemberBundle/Resouces/confit/services.xml is like this below.

<services>
    <service id="acme.memberbundle.calendar_listener" class="Acme\MemberBundle\EventListener\CalendarEventListener">
        <argument type="service" id="container" />
        <tag name="kernel.event_listener" event="calendar.load_events" method="loadEvents" />
    </service>

</services>

but it says 'The service "acme.memberbundle.calendar_listener" has a dependency on a non-existent service "container"'.

How can I pass my container to services?

this problem is related How to get userid from eventlistener which are called from AJAX

Community
  • 1
  • 1
whitebear
  • 11,200
  • 24
  • 114
  • 237
  • 1
    The name of the service is not `container`, it's `service_container`. I have fixed the answer from your original question. PS: I would recommend injecting the `security.context` directly instead of injecting the whole container. – igorw Jun 10 '13 at 14:44

1 Answers1

4

The container is registered as the service called "service_container", so you must pass "service_container" and not just "container":

<argument type="service" id="service_container" />

This is why currently Symfony doesn't understand what is the service "container" because there is no one registered with this name !

Sybio
  • 8,565
  • 3
  • 44
  • 53
  • it works.thanks for your good advice. I am not sure where 'id' I can find,but I will keep study. – whitebear Jun 10 '13 at 15:55
  • You can list all registered services by executing this command: php app/console container:debug ;) – Sybio Jun 10 '13 at 17:20