Register your Service in the Container

The MarkdownTransformer class is not an Autobot, nor a Decepticon, but it is a service because it does work for us. It's no different from any other service, like the markdown.parser, logger or anything else we see in debug:container... except for one thing: it sees dead people. I mean, it does not live in the container.

Nope, we need to instantiate it manually: we can't just say something like $this->get('app.markdown_transformer') and expect the container to create it for us.

Time to change that... and I'll tell you why it's awesome once we're done.

Open up app/config/services.yml:

10 lines app/config/services.yml
# Learn more about services, parameters and containers at
# parameter_name: value
# service_name:
# class: AppBundle\Directory\ClassName
# arguments: ["@another_service_name", "plain_value", "%parameter_name%"]

To add a new service to the container, you basically need to teach the container how to instantiate your object.

This file already has some example code to do this... kill it! Under the services key, give your new service a nickname: how about app.markdown_transformer:

10 lines app/config/services.yml
... lines 1 - 5
... lines 8 - 10

This can be anything: we'll use it later to fetch the service. Next, in order for the container to be able to instantiate this, it needs to know two things: the class name and what arguments to pass to the constructor. For the first, add class: then the full AppBundle\Service\MarkdownTransformer:

10 lines app/config/services.yml
... lines 1 - 5
class: AppBundle\Service\MarkdownTransformer
... lines 9 - 10

For the second, add arguments: then make a YAML array: []. These are the constructor arguments, and it's pretty simple. If we used [sunshine, rainbows], it would pass the string sunshine as the first argument to MarkdownTransformer and rainbow as the second. And that would be a very pleasant service.

In reality, MarkdowTransformer requires one argument: the markdown.parser service. To tell the container to pass that, add @markdown.parser:

10 lines app/config/services.yml
... lines 1 - 5
class: AppBundle\Service\MarkdownTransformer
arguments: ['@markdown.parser']

That's it. The @ is special: it says:

Woh woh woh, don't pass the string markdown.parser, pass the service markdown.parser.

And with 4 lines of code, a new service has been born in the container. I'm such a proud parent.

Go look for it:

./bin/console debug:container markdown

There is its! And it's so cute. Idea! Let's use it! Instead of new MarkdownTransformer(), be lazier: $transformer = $this->get('app.markdown_transformer):

125 lines src/AppBundle/Controller/GenusController.php
... lines 1 - 13
class GenusController extends Controller
... lines 16 - 58
public function showAction($genusName)
... lines 61 - 69
$markdownTransformer = $this->get('app.markdown_transformer');
... lines 71 - 97
... lines 99 - 123

When this line runs, the container will create that object for us behind the scenes.

Why add a Service to the Container

Believe it or not, this was a huge step. When you add your service to the container, you get two great thing. First, using the service is so much easier: $this->get('app.markdown_transformer). We don't need to worry about passing constructor arguments: heck it could have ten constructor arguments and this simple line would stay the same.

Second: if we ask for the app.markdown_transformer service more than once during a request, the container only creates one of them: it returns that same one object each time. That's nice for performance.

Oh, and by the way: the container doesn't create the MarkdownTransformer object until and unless somebody asks for it. That means that adding more services to your container does not slow things down.

The Dumped Container

Ok, I have to show you something cool. Open up the var/cache directory. If you don't see it - you may have it excluded: switch to the "Project" mode in PhpStorm.

Open var/cache/dev/appDevDebugProjectContainer.php:

3709 lines var/cache/dev/appDevDebugProjectContainer.php
... lines 1 - 9
* appDevDebugProjectContainer.
* This class has been auto-generated
* by the Symfony Dependency Injection Component.
class appDevDebugProjectContainer extends Container
... lines 18 - 3707

This is the container: it's a class that's dynamically built from our configuration. Search for "MarkdownTransformer" and find the getApp_MarkdownTransformerService() method:

3709 lines var/cache/dev/appDevDebugProjectContainer.php
... lines 1 - 15
class appDevDebugProjectContainer extends Container
... lines 18 - 23
public function __construct()
... lines 26 - 32
$this->methodMap = array(
... line 34
'app.markdown_transformer' => 'getApp_MarkdownTransformerService',
... lines 36 - 238
... lines 240 - 249
... lines 251 - 272
* Gets the 'app.markdown_transformer' service.
* This service is shared.
* This method always returns the same instance of the service.
* @return \AppBundle\Service\MarkdownTransformer A AppBundle\Service\MarkdownTransformer instance.
protected function getApp_MarkdownTransformerService()
return $this->services['app.markdown_transformer'] = new \AppBundle\Service\MarkdownTransformer($this->get('markdown.parser'));
... lines 285 - 3707

Ultimately, when we ask for the app.markdown_transformer service, this method is called. And look! It runs the same PHP code that we had before in our controller: new MarkdownTransformer() and then $this->get('markdown.parser') - since $this is the container.

You don't need to understand how this works - but it's important to see this. The configuration we wrote in services.yml may feel like magic, but it's not: it causes Symfony to write plain PHP code that creates our service objects. There's no magic: we describe how to instantiate the object, and Symfony writes the PHP code to do that. This makes the container blazingly fast.

Leave a comment!

  • 2017-04-25 jian su

    Make total Sense~! Services is awesome, it did a lot of dirty work behind the scenes! But The type hint things is kinda suck since I need to dig deep and find the base class, and then even i found it, there are multiple implements such as Cache, FlushableCache, ClearableCache, MultiGetCache, MultiPutCache. Even I am luck I found the Cache.. it also implement mutiple places...and then I need to find the one has save() and contains... you know what i mean.. any better way?

  • 2017-04-25 weaverryan

    Hey jian su!

    Good question. You *cannot* remove it, and for a very important reason :). In services.yml, thanks to arguments: ['@markdown.parser'], when we call $this->get('app.markdown_transformer'), behind the scenes, the container IS instantiating our MarkdownTransformer and it IS passing it the markdown.parser as the first argument. Basically, imagine that, behind the scenes, this is happening (because it IS!):

    new MarkdownTransformer($this->container->get('markdown.parser'));

    So even though we are not explicitly passing the argument to ourselves, the container *is* still passing this. So, it's definitely needed.

    Does that make sense?

  • 2017-04-25 jian su

    weaverryan If transformer is not passing markdown_transformer object into construct since we are using services in our services.yml. Can I safely remove construct from our MarkdownTransformer class?

  • 2016-09-02 Jesus Gabriel Jimenez Blanco

    That was it!
    Thank you so much for the detailed explanation, that really helps becoming a better programmer!

    I'm really enjoying this tutorial and it's so awesome that we can count on you whenever there are errors!
    Thank you again!

  • 2016-09-02 weaverryan

    Hey Jesus!

    Oh man, I know this error! And it's *super* hard to debug the first time you see it.

    First, the solution: you're missing the "use" statement in MarkdownTransformer for the MarkdownParserInterface.

    Now, the explanation :). Because the "use" statement is missing, PHP assumes that MarkdownParserInterface must be a class that lives in the same namespace as the class that it's currently inside of. In other words, it assumes that the class is AppBundle\Service\MarkdownParserInterface. Then, the error "kind of" makes sense, it's saying:

    > Hey! This object, which is an instance of Max, is being passed to __construct(), but you're expecting some (non-existent) MarkdownParserInterface instance!

    Technically, you *really* want PHP to yell: "Yo! What is this MarkdownParserInterface!? That's not a real class?". But, it doesn't.

    So easy fix, but I give it a long explanation because this is *such* a tough one to debug, and - hopefully - next time you'll debug it immediately.

    I hope that helps! Or, if I'm *completely* wrong, you can tell me ;).


  • 2016-09-01 Jesus Gabriel Jimenez Blanco

    Hello! i'm getting this error:

    Type error: Argument 1 passed to
    AppBundle\Service\MarkdownTransformer::__construct() must be an instance
    of AppBundle\Service\MarkdownParserInterface, instance of
    Knp\Bundle\MarkdownBundle\Parser\Preset\Max given, called in
    on line 292

    It's like it can't cast from Max instance given when called the contruct function to the MarkdownParserInterface even though Max implements MarkdownParserInterface... I don't understand why does it happen, it also happened in the previous page... I tried to solve it myself but it's just like you can't actually cast between those two...

    If anyone could help I would really appreciate it. Thanks in advance!