2

According to the documentation:

The container is available in any traditional Symfony2 controller where you can access the services of the container via the get() shortcut method

So, I have managed to invoke and use JMSSerializer within a Controller by calling:

$serializer = $this->get('serializer');

However, how can I invoke the container in a custom class? The same command fails indicating Fatal Error for calling undefined method get().

room13
  • 1,922
  • 1
  • 15
  • 28
DVC
  • 29
  • 1
  • 2
  • Can you expand a little on what you mean by "custom class"? Does that class have access to the Service Container? – Thomas Kelley Nov 09 '12 at 16:15
  • Let's say that I want to make a class within my bundle which uses JMSSerializer. How can I make it have access to the Service Container? – DVC Nov 09 '12 at 16:20

1 Answers1

6

This is exactly what dependency injection is for. Your "custom class" has a dependency on the "serializer" service. So, you should define your class as a service in the service container

app/config/config.yml

services:
    my_custom_class:
        class:        My\RandomBundle\CustomClass
        arguments:    [serializer]

My\RandomBundle\CustomClass

use JMS\SerializerBundle\Serializer\Serializer;

class CustomClass
{
    private serializer;

    public function __construct(Serializer $serializer)
    {
        $this->serializer = $serializer;
    }
}

Now you can get your custom class from the container wherever it's used and it will automatically have the serializer available to it.

$myServiceWithASerializer = $this->container->get('my_custom_class');

The docs describe this as well:
http://symfony.com/doc/current/book/service_container.html#creating-configuring-services-in-the-container

MDrollette
  • 6,887
  • 1
  • 36
  • 49
  • Thanks for the response. I understand the above concept, but I have actually not described well my question. If I was within a Controller I could use $serializer = $this->get('serializer') or $myServiceWithASerializer = $this->container->get('my_custom_class') from your example and start playing with JMSSerializer. However, if I make a different class in my bundle (i.e. MyClass) how can I invoke this service? – DVC Nov 09 '12 at 17:15
  • It would be the same concept. You would either need to pass the serializer in every time you construct MyClass or define it as a service so it will be done automatically. – MDrollette Nov 09 '12 at 17:53
  • Still my question is not answered. When I call the line: $myServiceWithASerializer = $this->container->get('my_custom_class'); I get an Undefined property warning for the $container variable. I call it within a function of some random class in my bundle. What am I missing here? – DVC Nov 10 '12 at 01:16
  • There's no magic involved. If you don't define $this->container then it won't exist. I just used that as an example. The point is, you don't pass the entire service container in to your custom classes. You use the DIC to define your classes dependencies, then when you want to use your classes you fetch them from the DIC. http://symfony.com/doc/current/book/service_container.html – MDrollette Nov 10 '12 at 04:23
  • I instantiate the container and try to invoke my service: $container = new Container(); $serializer = $container->get('my_custom_class'); but I get: You have requested a non-existent service "my_custom_class". What's my mistake here? – DVC Nov 10 '12 at 09:51