If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeOoh, bonus feature! In services.yml
, remove arguments
and instead just say autowire: true
:
... lines 1 - 5 | |
services: | |
... lines 7 - 10 | |
app.markdown_extension: | |
... 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:
... 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.
Tip
The autowiring logic has changed in Symfony 3.3 and higher. For more info, we have a tutorial! https://knpuniversity.com/screencast/symfony-3.3/autowiring-logic
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.
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
:
... 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.
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!
// composer.json
{
"require": {
"php": ">=5.5.9",
"symfony/symfony": "3.1.*", // v3.1.4
"doctrine/orm": "^2.5", // v2.7.2
"doctrine/doctrine-bundle": "^1.6", // 1.6.4
"doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
"symfony/swiftmailer-bundle": "^2.3", // v2.3.11
"symfony/monolog-bundle": "^2.8", // 2.11.1
"symfony/polyfill-apcu": "^1.0", // v1.2.0
"sensio/distribution-bundle": "^5.0", // v5.0.22
"sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
"incenteev/composer-parameter-handler": "^2.0", // v2.1.2
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"knplabs/knp-markdown-bundle": "^1.4", // 1.4.2
"doctrine/doctrine-migrations-bundle": "^1.1" // 1.1.1
},
"require-dev": {
"sensio/generator-bundle": "^3.0", // v3.0.7
"symfony/phpunit-bridge": "^3.0", // v3.1.3
"nelmio/alice": "^2.1", // 2.1.4
"doctrine/doctrine-fixtures-bundle": "^2.3" // 2.3.0
}
}