Chapters
23 Chapters
|
2:20:28
|
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!
10.
Autowiring Dependencies into a Service
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 great for Symfony 6!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.3.0 || ^8.0.0",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
"sensio/framework-extra-bundle": "^6.0", // v6.2.1
"sentry/sentry-symfony": "^4.0", // 4.0.3
"symfony/asset": "5.0.*", // v5.0.11
"symfony/console": "5.0.*", // v5.0.11
"symfony/debug-bundle": "5.0.*", // v5.0.11
"symfony/dotenv": "5.0.*", // v5.0.11
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/framework-bundle": "5.0.*", // v5.0.11
"symfony/monolog-bundle": "^3.0", // v3.6.0
"symfony/profiler-pack": "*", // v1.0.5
"symfony/routing": "5.1.*", // v5.1.11
"symfony/twig-pack": "^1.0", // v1.0.1
"symfony/var-dumper": "5.0.*", // v5.0.11
"symfony/webpack-encore-bundle": "^1.7", // v1.8.0
"symfony/yaml": "5.0.*" // v5.0.11
},
"require-dev": {
"symfony/maker-bundle": "^1.15", // v1.23.0
"symfony/profiler-pack": "^1.0" // v1.0.5
}
}
17 Comments
Hey there
It looks like you're calling an unknown property
code, you should make the call through your cache property$this->cache->get(...);Cheers!
Hey, in our MarkdownHelper's constructor you can type even less -- if you are using PHP 8 -- by using contructor property promotion. https://www.php.net/manual/....
btw I am LOVING these tutorials and Symfony. I am coming from the Laminas (formerly Zend) framework where you have to write your own factories and manually register them in configuration so that the container will know how to instantiate them for you. Blaugh!
Woo for PHP 8! In our Symfony 6 tutorials starting in January, we can finally use PHP 8 (we always try to use the minimum-required version), so life will be better!
> btw I am LOVING these tutorials and Symfony
Welcome to Symfony - happy to have you :). And I see you're a fellow runner! I don't run as much anymore (I have a 5 year old), but I LOVE being in shape to get out there and just... go...
Cheers!
Somewhere along the way I must have messed up something, but I can't for the life of me figure out where. I have followed the tutorials, but when I try to run the service it is telling me that my value of the parse is null:
Return value of App\Service\MarkdownHelper::parse() must be of the type string, null returned
But, in the stacktrace I am seeing this:
MarkdownHelper->parse('I\'ve been turned into a cat, any thoughts on how to turn back? While I\'m adorable, I don\'t really care for cat food.')
in src/Controller/QuestionController.php (line 41)
Which sure looks like a string to me....
Here is my parse function
`
`
I should note that I think it has to do with the cache. When I tried to implement that before, it was pulling up blank before as well. If I remove the cache from the function, I do get it to work properly.
So I guess the question is why is the cache not working? I am on PHP 7.3.27.
Hey Tim C.
It may depend on you current cache service configuration, try to debug
$this->cachewhich adapter do you use?Cheers!
Symfony\Component\Cache\Adapter\TraceableAdapter {#267 â–¼
#pool: Symfony\Component\Cache\Adapter\FilesystemAdapter {#260 â–¼
-createCacheItem: Closure($key, $value, $isHit) {#263 â–¼
class: "Symfony\Component\Cache\CacheItem"
}
-mergeByLifetime: Closure($deferred, $namespace, &$expiredIds) {#265 â–¼
class: "Symfony\Component\Cache\CacheItem"
use: {â–¼
$getId: Symfony\Component\Cache\Adapter\AbstractAdapter::getId($key) {#262 …}
$defaultLifetime: 0
}
}
-namespace: ""
-namespaceVersion: ""
-versioningIsEnabled: false
-deferred: []
-ids: []
#maxIdLength: null
#logger: Symfony\Bridge\Monolog\Logger {#264 â–¼
#name: "cache"
#handlers: array:2 [â–¶]
#processors: array:1 [â–¶]
#microsecondTimestamps: true
#timezone: DateTimeZone {#266 â–¶}
#exceptionHandler: null
}
-callbackWrapper: array:2 [â–¼
0 => "Symfony\Component\Cache\LockRegistry"
1 => "compute"
]
-computing: []
-marshaller: Symfony\Component\Cache\Marshaller\DefaultMarshaller {#261 â–¼
-useIgbinarySerialize: false
}
-directory: "/var/www/aqua_note/var/cache/dev/pools/BnBSC5y2vY/"
-tmp: null
}
-calls: []
}
Hey Tim C.
Sorry for late reply, ok you are using filesystem adapter, so is it possible that
/var/www/aqua_note/var/cache/dev/pools/BnBSC5y2vY/is not writable? Have you tried to completely removevar/cache/dev/?Cheers!
Lol, nice attempt to attack Laravel. And no, Facades aren't recipes for bad code rofl.
Hey @disqus_1a9hby0y3a!
That's fair, though this isn't meant as an attack on Laravel, just a difference in philosophy :). In Symfony, we choose to "force" dependency injection. On the "bad" hand, that makes learning how to use Symfony harder, which is why we've added autowiring to alleviate that. In the good side, if you're getting started, it forces you into the very traditional design pattern of dependency injection. One advantage of this (that I personally really like) is that you can get a feel for "what a class does and does not do" by looking at its constructor. If I look at a class where the constructor does NOT have a MailerInterface, then I know that this service definitely does NOT send emails. If you're able to use a static method to fetch a service, then it's possible that there is a mailer call hiding way down on line 150 for your class.
So I absolutely think that you can write awesome code in any framework - definitely including Laravel! In Symfony, we've chosen to be a bit more strict. It doesn't mean that Laravel Facades == bad code. But it's harder for users to accidentally start writing worse code without the static helpers.
Anyways, thanks for the conversation - I think we'll ultimately disagree, but that's ok :). I like Laravel - we've borrowed many great ideas from it.
Cheers!
Hi colleagues, whenever I want to get a container in my constructor, I'm getting this:
The reasonable question, how to solve this then? What means "explicitly" in this case? Probably in future versions this way of injecting will be removed.
Hey WebAdequate,
Injecting the whole container is not a good practice anymore. If you think you still need to inject it - see this answer: https://stackoverflow.com/a... . But it's a good idea to refactor your code and inject only *direct* (more specific) dependencies you need into your services. If you need to inject a lot of services, sometimes it happens on practice - take a look at Service Subscribers: https://symfony.com/doc/mas...
I hope this helps!
Cheers!
Guys, I'm trying according with https://symfony.com/doc/current/service_container/synthetic_services.html to create a synthetic service: but i got an error:
<blockquote>The "synthetic_service" service is private, you cannot replace it.
</blockquote>
Any indeas?
I have on servises.yaml:
Thanks!
Hey triemli
do you have already defined the service
synthetic_servicesomewhere else? Can I see yourconfig/services.yamlfile?Actually was resolved with adding *public: true* to the synthetic_service
Hey triemli!
Synthetic services are pretty uncommon, but your solution makes sense to me. By definition, a synthetic service is one that you set at runtime, which means that you call $container->set(). It makes sense that it would need to be public (which allows it to be fetched via $container->get()) . Basically, if you make something private, Symfony does all kinds of build-time optimizations which simply don't make sense for a synthetic service.
Cheers!
"Houston: no signs of life"
Start the conversation!