Chapters
29 Chapters
|
1:34:37
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: >=5.3.3
Subscribe to download the code!Compatible PHP versions: >=5.3.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
24.
Dependency Inject All the Things
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.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.4", // v2.4.2
"doctrine/orm": "~2.2,>=2.2.3", // v2.4.2
"doctrine/doctrine-bundle": "~1.2", // v1.2.0
"twig/extensions": "~1.0", // v1.0.1
"symfony/assetic-bundle": "~2.3", // v2.3.0
"symfony/swiftmailer-bundle": "~2.3", // v2.3.5
"symfony/monolog-bundle": "~2.4", // v2.5.0
"sensio/distribution-bundle": "~2.3", // v2.3.4
"sensio/framework-extra-bundle": "~3.0", // v3.0.0
"sensio/generator-bundle": "~2.3", // v2.3.4
"incenteev/composer-parameter-handler": "~2.0", // v2.1.0
"doctrine/doctrine-fixtures-bundle": "~2.2.0", // v2.2.0
"ircmaxell/password-compat": "~1.0.3", // 1.0.3
"phpunit/phpunit": "~4.1", // 4.1.0
"stof/doctrine-extensions-bundle": "~1.1.0" // v1.1.0
}
}
2 Comments
Why not extend the Container on the service class?
Oscar!
You're my favorite today. This is a *very* insightful question. The trick of extending the Controller and magically getting access to Symfony's container *only* works for controller classes. Basically, your controller is the only object where you *can't* control how it's instantiated: Symfony always (behind the scenes) instantiates a new instance of your controller and passes it *no* constructor arguments. In order to get access to the container, Symfony checks to see if your controller extends the base controller (technically, if it implements this thing called the ContainerAwareInterface - http://knpuniversity.com/sc.... If it does, it passes the container to our controller, so that we have access to all of our useful objects.
But, that doesn't happen with services: we're in complete control of how we're instantiated (via services.yml), so we just tell Symfony exactly what we need. This also helps keep our services focused: instead getting passed the entire container (which admittedly, would be simple at first), we dependency-inject only the exact objects we need. The cool part about this is that when I look at your service object later, I immediately understand what it does and what it does *not* do. For example, if I don't see some Mailer object in your constructor, I can safely assume that your service does not send emails (and if I'm debugging some email problem, I can move on to look somewhere else).
Let me know if this helps! Cheers!
"Houston: no signs of life"
Start the conversation!