Chapters
26 Chapters
|
2:53:08
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
08.
How does the Controller Access the Container?
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial also works well for Symfony 6!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-iconv": "*",
"antishov/doctrine-extensions-bundle": "^1.4", // v1.4.3
"aws/aws-sdk-php": "^3.87", // 3.133.20
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.12.1
"doctrine/doctrine-bundle": "^2.0", // 2.2.3
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // 2.2.2
"doctrine/orm": "^2.5.11", // 2.8.2
"easycorp/easy-log-handler": "^1.0", // v1.0.9
"http-interop/http-factory-guzzle": "^1.0", // 1.0.0
"knplabs/knp-markdown-bundle": "^1.7", // 1.9.0
"knplabs/knp-paginator-bundle": "^5.0", // v5.4.2
"knplabs/knp-snappy-bundle": "^1.6", // v1.7.1
"knplabs/knp-time-bundle": "^1.8", // v1.16.0
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.24
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"league/html-to-markdown": "^4.8", // 4.9.1
"liip/imagine-bundle": "^2.1", // 2.5.0
"oneup/flysystem-bundle": "^3.0", // 3.7.0
"php-http/guzzle6-adapter": "^2.0", // v2.0.2
"phpdocumentor/reflection-docblock": "^5.2", // 5.2.2
"sensio/framework-extra-bundle": "^5.1", // v5.6.1
"symfony/asset": "5.0.*", // v5.0.11
"symfony/console": "5.0.*", // v5.0.11
"symfony/dotenv": "5.0.*", // v5.0.11
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "5.0.*", // v5.0.11
"symfony/framework-bundle": "5.0.*", // v5.0.11
"symfony/mailer": "5.0.*", // v5.0.11
"symfony/messenger": "5.0.*", // v5.0.11
"symfony/monolog-bundle": "^3.5", // v3.6.0
"symfony/property-access": "5.0.*|| 5.1.*", // v5.1.11
"symfony/property-info": "5.0.*|| 5.1.*", // v5.1.10
"symfony/routing": "5.1.*", // v5.1.11
"symfony/security-bundle": "5.0.*", // v5.0.11
"symfony/sendgrid-mailer": "5.0.*", // v5.0.11
"symfony/serializer": "5.0.*|| 5.1.*", // v5.1.10
"symfony/twig-bundle": "5.0.*", // v5.0.11
"symfony/validator": "5.0.*", // v5.0.11
"symfony/webpack-encore-bundle": "^1.4", // v1.11.1
"symfony/yaml": "5.0.*", // v5.0.11
"twig/cssinliner-extra": "^2.12", // v2.14.3
"twig/extensions": "^1.5", // v1.5.4
"twig/extra-bundle": "^2.12|^3.0", // v3.3.0
"twig/inky-extra": "^2.12", // v2.14.3
"twig/twig": "^2.12|^3.0" // v2.14.4
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.4.0
"fakerphp/faker": "^1.13", // v1.13.0
"symfony/browser-kit": "5.0.*", // v5.0.11
"symfony/debug-bundle": "5.0.*", // v5.0.11
"symfony/maker-bundle": "^1.0", // v1.29.1
"symfony/phpunit-bridge": "5.0.*", // v5.0.11
"symfony/stopwatch": "^5.1", // v5.1.11
"symfony/var-dumper": "5.0.*", // v5.0.11
"symfony/web-profiler-bundle": "^5.0" // v5.0.11
}
}
7 Comments
I don't understand the passage at 1:10, when you say: who's populating the this->container property?,
and then you talk about parameter_bag.
Is there a link between how the container is set and the parameter_bag? Or are they two different topics?
Hey Pierre!
Ah, I can see how that is confusing! Before I explain WHO is setting $this->container, I start talking about the "parameter_bag" service to try to show you how "strange" this container property is. But I'm not YET explaining who sets it... and "parameter_bag" and $this->container are two different topics. The MOST important thing is really $this->container, which is set thanks to the service subscriber magic. The "parameter_bag" is simply ONE service that is in this mini-container.
Let me know if that make sense - I can totally see how that was confusing... I went on a tangent!
Cheers!
I'm fascinated by
@requiredannotation forAbstractController::setContainermethod. It basically removes the need to declare the constructor in AbstractController class. When extending AbstractController in my child controller class, I have an opportunity to have my own constructor without theparent::__construct(ContrainerInterface $container);call. Convenient!I wonder, and this is my question, what are other possible situations when @required annotation may come in handy? It's a cool tool but I don't really feel how to properly use it. E.g. is it possible to use
@requiredwith service factory when creating individual services with preset configuration values?Hey Volodymyr T.!
It's neat, isn't it!
It does need to be used with caution. The reason is that, if you allow a dependency to be set via a setter method, then from an object-oriented level, it's not actually required. What I mean is, if someone tried to use your class outside of Symfony, then they might forget to call the setter and your code might explode (because it expects that property to be set). That is probably not a very realistic situation... but it's kind of a sign of "code smell": if a dependency is needed by your class, then you're sorta supposed to "force" it to be passed via a required constructor argument. So for me, the most common place I use this is to inject the logger because that's really an optional dependency - https://symfonycasts.com/screencast/symfony4-fundamentals/logger-trait.
Ummm, I'm not sure! If you added
@requiredabove a method of a service factory, then Symfony would call that method before instantiating the factory (and so before it calls the method to create the real service). But I don't think I understand what you mean by "with preset configuration values". What trick are you thinking of? :)Cheers!
Why there is a '?' in method getSubscribedServices ?
Hey Thomas A.
It means that such dependency is optional. In other words, if for any reason you don't have, let's say, the Serializer component installed, then your app won't explode but you should not ask for such service anyways
Cheers!
OK, thanks!
"Houston: no signs of life"
Start the conversation!