Chapters
25 Chapters
|
2:34:05
|
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!
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": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"knplabs/knp-time-bundle": "^1.18", // v1.19.0
"symfony/asset": "6.1.*", // v6.1.0-RC1
"symfony/console": "6.1.*", // v6.1.0-RC1
"symfony/dotenv": "6.1.*", // v6.1.0-RC1
"symfony/flex": "^2", // v2.4.5
"symfony/framework-bundle": "6.1.*", // v6.1.0-RC1
"symfony/http-client": "6.1.*", // v6.1.0-RC1
"symfony/monolog-bundle": "^3.0", // v3.8.0
"symfony/runtime": "6.4.3", // v6.4.3
"symfony/twig-bundle": "6.1.*", // v6.1.0-RC1
"symfony/ux-turbo": "^2.0", // v2.1.1
"symfony/webpack-encore-bundle": "^1.13", // v1.14.1
"symfony/yaml": "6.1.*", // v6.1.0-RC1
"twig/extra-bundle": "^2.12|^3.0", // v3.4.0
"twig/twig": "^2.12|^3.0" // v3.4.0
},
"require-dev": {
"symfony/debug-bundle": "6.1.*", // v6.1.0-RC1
"symfony/maker-bundle": "^1.41", // v1.42.0
"symfony/stopwatch": "6.1.*", // v6.1.0-RC1
"symfony/web-profiler-bundle": "6.1.*" // v6.1.0-RC1
}
}
22 Comments
Hey SymfonyCast Team!
About your Dependency Injection Container with Autowiring and I believe Reflections. Does Symfony use PHP-DI, Symfony-DI or both with similar features. I was also curious about how you were able to get the terminal to display data about the Service Container and Parameter Container. I'd like to learn more about it and try to build one as a project for learning purposes. Would it be too much of an ask, by asking, if you would be able to provide a great source of learning material regarding Dependency Injection Container and how to use the terminal to display Dependency Injection Container data. If so that would be awesome! I'm also interested in becoming a Certified Symfony Developer by learning more about the Symfony Platform and what it has to offer. I wish you guys had a location based in california that would allow me to volunteer my services...
Hey @Akili!
Very interesting questions! Let me do my best to answer :)
Yup, reflection is definitely used internally to determine the type-hint of an autowireable argument.
Symfony has its own
symfony/dependency-injectioncomponent that holds all of this logic - it doesn't use PHP-DI.This is one of the super-powers of Symfony's dependency injection system. It builds up ALL of the metadata of what every service is and every argument to every service. And so, dumping a list of this then becomes quite easy. Internally, this metadata is known as the
ContainerBuilder. TheContainerBuilderis aware of EVERY service and EVERY argument.We have a very old tutorial - based on Symfony 3 - that talks a lot about this - https://symfonycasts.com/screencast/symfony-journey-di - the code will be out of date, and it doesn't show how autowiring works (iirc, it was built before autowiring exists in Symfony) but it will show the fundamentals of the dependency injection system and how the container is built.
A fellow USA person! I'm based on Michigan :)
Cheers!
Thanks Ryan!
I really appreciate you for this.
Hi.
Can anyone explain why B is wrong answer in the next challenge:
Which of the following is NOT a correct way to use dependency injection:
Hey t5810,
The correct answer is
Abecause theCacheInterface $cacheis just an argument of the method, which technically is a dependency of the class, but it's not a property, so only that method depends on it but not the whole class. You may ask, but we do the same to Symfony controllers, yes, but they're a special kind of service, the Symfony container takes care of "injecting" all of the controller's method argumentsI hope this makes sense to you :) Cheers!
Hi MolloKhan
I re-watched the video again, I re-read the question 3 times and THEN I noticed the bold and capital NOT in the question....
Sorry for wasting your time...
Regards
haha, it happens! You're welcome :)
Hello, I have a question: why in this case we do not also inject CacheItemInterface? Why HttpClientInterface and CacheInterface are injected as dependencies and CacheItemInterface is inserted into the findAll() method as an argument?
Hey @Alin-D
The
$cacheItemobject it's not a dependency of theMixRepositoryclass. It's an argument you get passed by the callback function of the$this->cache->get()method call. In other words, the second argument of that method is a callback function, and it will pass to you an instance ofCacheItemInterface. I hope it's more clear nowCheers!
In which cases I have to create my own services? Is there a guideline for that?
Hey Stefaaan,
I think this tutorial should answer this question :) In short, if you need some job to do, and you would like to be able to re-use it in a few places or test it - there is a good plan to create a service for this.
Cheers!
I've been following along with these great tutorials and find myself a bit stuck. For this example I have 3 classes:
services.yaml:
When I try to execute the code I get:
Uncaught Error: Too few arguments to function App\AbstractClassThree::__construct(), 0 passed in /src/AbstractClassTwo on line 35 and exactly 1expected.
Any help much appreciated!
Howdy Alexander!
I edited your post just to make it a tad bit easier to read the classes and what they are doing. Anywho, I spent a few minutes playing around with this use case and I think I see what the problem is.
Symfony is great managing parent services when they only extended one level. E.g. Class One extends Abstract Class Two. But when we throw in a second abstraction level into the mix, we start hitting limitations with Symfony's auto-wiring and PHP's inheritance.
Because
AbstractClassTwoextendsAbstractClassThreeand both of them haveconstructors(), I believe you will need to pass in the arguments forThreeintoTwo's constructor like so:Otherwise
Three's constructor is never called. Even when I tried your example using the "Common Dependency Method" shown in the docs: https://symfony.com/doc/current/service_container/parent_services.html - I had the same issue.I Hope this helps!
Thanks for the response and the time to dig in to find a solution. I also noticed in the docs (just above this line: https://symfony.com/doc/current/service_container/injection_types.html#immutable-setter-injection):
I had also gotten your solution to work, it just seemed a little ugly compared with the way dependency injection usually seems to work.
after the one years of struggling with Symfony first time i start to understand what Symfony is? and how it works?
many thanks for your work!
Hey Kaan,
Is it a question? :) Like do you want to know if SymfonyCasts tutorials will help you to understand Symfony framework better after a year of its learning on our platform? Or are you just leaving feedback about SymfonyCasts?
Cheers!
it was actually a compliment sir..
i watched plenty of videos like 'symfony for beginners' or 'creating a blog system with symfony' but none of them explain what Symfony is. before watch your videos i was know to how to code with Symfony but i had no idea about how that machine works : )
thank you for your effort and forgive my terrible English..
i hope to made myself clear : )
Hey Kaan,
Ah, I see! The question marks in your first message confused me a bit :)
Thank you for your kind words about SymfonyCasts - that's exactly what our mission is, we're trying to explain complex things in a simple and understandable way for everyone, even newcomers :) So, we're really happy to hear our service is useful for you ;)
Cheers!
Bonjour!!
If I would like to use loop in my navbar (dropdown-menu) to show all the availble elements how I can declare a global variable in this case?
`I did that way:
I have difficulty because I can't use findAll() method easily outside the repository and my project is very big so I can't declare it in every single page.
Any idea or soloution about that please?
Many thanks for your help :)
Hey Lubna,
If you have a static value for the global variable or when you need to make some env var global in twig - you can use this way: https://symfony.com/doc/current/templating/global_variables.html
But if you need a data from your repository - probably that global Twig var won't help you. In this case, you can create a custom Twig function that will return whatever you want, we're talking about custom Twig functions here: https://symfonycasts.com/screencast/symfony4-doctrine/twig-extension
Or you can check the Symfony's Twig docs about it.
Cheers!
Hi! Thanks for understanding my problem correctly.
Gonna try your suggestion!
Best,
Lubna
Hey Lubna,
You're welcome! Good luck with it ;)
Cheers!
"Houston: no signs of life"
Start the conversation!