Ooh, bonus feature! In services.yml, remove arguments and instead just say autowire: true:

17 lines app/config/services.yml
... lines 1 - 5
... lines 7 - 10
... lines 12 - 14
#arguments: ['@app.markdown_transformer']
autowire: true

Refresh again. It still works! But how? We didn't tell Symfony what arguments to pass to our constructor? What madness is this!? With autowire: true, Symfony reads the type-hints for each constructor argument:

33 lines src/AppBundle/Twig/MarkdownExtension.php
... lines 1 - 6
class MarkdownExtension extends \Twig_Extension
... lines 9 - 10
public function __construct(MarkdownTransformer $markdownTransformer)
... lines 12 - 31

And tries to automatically find the correct service to pass to you. In this case, it saw the MarkdownTransformer type-hint and knew to use the app.markdown_transformer service: since that is an instance of this class. You can also type-hint interfaces.

This doesn't always work, but Symfony will give you a big clear exception if it can't figure out what to do. But when it does work, it's a great time saver.

Auto-Escaping a Twig Filter

The HTML is still being escaped - I don't want to finish before we fix that! We could add the |raw filter... but let's do something cooler. Add a third argument to Twig_SimpleFilter: an options array. Add is_safe set to an array containing html:

35 lines src/AppBundle/Twig/MarkdownExtension.php
... lines 1 - 6
class MarkdownExtension extends \Twig_Extension
... lines 9 - 15
public function getFilters()
return [
new \Twig_SimpleFilter('markdownify', array($this, 'parseMarkdown'), [
'is_safe' => ['html']
... lines 24 - 33

This means it's always safe to output contents of this filter in HTML. Refresh one last time. Beautiful.

Where now!?

Oh my gosh guys! I think you just leveled up: Symfony offense increased by five points. Besides the fact that a lot more things will start making sense in Symfony, you also know everything you need to start organizing your code into service classes - that whole service-oriented architecture thing I was talking about earlier. This will lead you to wonderful applications.

There's really nothing that we can't do now in Symfony. In the next courses, we'll use all this to master new tools like forms and security. Seeya next time!

Leave a comment!

  • 2017-07-10 Wei Dai

    solved. Thanks for the tutorial.

  • 2017-07-08 Wei Dai

    After run the console command, the container doesn't know any classes from the Knp markdown bundle. I think the reason is because the bundle is built for previous symfony version, and all service makred as non-public?
    The container can only know the customer classes we defined.

  • 2017-07-03 Diego Aguiar

    Hey Wei Dai

    Symfony 3.3 changed a lot of things about autowiring and the container, we have a dedicated tutorial just in case you want to go deeper

    If you run "bin/console debug:container --types" you can see all the classes that your container knows about, so you can find which ones are for the MarkdownParser, and then, define an alias that you can inject into your service.
    You can filter the list by running this: "bin/console debug:container --types | grep Markdown "


  • 2017-07-03 Wei Dai

    I have the same problem. And I believe it is only for the latest symphony 3.3.2.

  • 2017-06-20 Diego Aguiar

    Hey Praxitelis Kourtellos

    I believe you are trying to autowire the service "app.markdown_transformer", but that's not correct, you should autowire the other service "app.markdown_extension", if I'm wrong, can you show me your services.yml file ?


  • 2017-06-20 Praxitelis Kourtellos

    Any idea about this error:

    Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\AutowiringFailedException: Cannot autowire service "AppBundle\Service\MarkdownTransformer": argument "$markdownParser" of method "__construct()" references interface "Knp\Bundle\MarkdownBundle\MarkdownParserInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "markdown.parser.min", "markdown.parser.light", "markdown.parser.medium", "markdown.parser.max", "markdown.parser.flavored". in C:\Users\pkourtellos\Desktop\Php stuff\aqua_note\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\AutowirePass.php:291
    Stack trace:
    #0 C:\Users\pkourtellos\Desktop\Php stuff\aqua_note\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\AutowirePass.php(223): Symfony\Component\DependencyInjection\Compiler\AutowirePass->autowireMethod(Object(ReflectionMethod), Array)
    #1 C:\Users\pkourtellos\Desktop\Php stuff\aqua_note\vendor\symfony\symfony\src\Symfony\Component\Dependen in C:\Users\pkourtellos\Desktop\Php stuff\aqua_note\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\AutowirePass.php on line 291

    I've even taken out autowire=true from services.yml

  • 2016-12-31 Felipe Gusmao

    While interviewing for few jobs I was asked about implementing a service bus. I understand DIP and services. However, I cant see how a service bus could be implemented. Do you guys from Knp, know of an example of service bus and its use case. Possibly, using RabbitMQ?

  • 2016-08-10 Victor Bocharsky


    Ah, DependencyInjection is the most mystery dir in bundle structure at first sight :). First of all you need to understand what is a Symfony bundle. You could also check this course, which explains basic things about this directory. In short, this helps to plug in third-party bundles in your application (imports services, routes, configuration, etc.).

    Usually, model is a simple data class which you used in code. It looks like an entity, but doesn't store in DB (doesn't handle with Doctrine). Frequently, in Symfony projects, models uses in form type as data classes.


  • 2016-08-10 3amprogrammer

    I am exploring and I am confused about the dir structure. What the heck are DependencyInjection and Model directories in Bundles? Do we have any vid on this topic here?

  • 2016-06-15 JLChafardet

    I'll give it some thought and let you know, as indeed, I think this should be explained further, deeply explored, maybe a tutorial with a more hands on, real life situation would help, who knows. Disquss integration for a blog, or a simple "invoice" manager (not a billing system, but a invoice generator, where you mark them as pending, unpaid, canceled, paid, offer printable version, pdf export, send through email, whatever, just blabling on the-spot ideas)

  • 2016-06-15 weaverryan

    I totally agree on both points. But I'm not sure *what* else might be useful to cover. In its simplest sense, it's "put logic into services!" and this tutorial showed the mechanics for doing that. If we can think of a few concrete items that would be helpful, then yea - let's do a tutorial on it :)

  • 2016-06-15 JLChafardet

    Hey there weaverryan

    are there any plans to delve deeper into this topic? (SOA in symfony) ? It was a wonderful topic, yet we just scratched the surface and it felt a VERY important topic to master.