From the course: Symfony 6 Essential Training

Unlock the full course today

Join today to access over 25,100 courses taught by industry experts.

Service container

Service container

- [Instructor] When building an app, it's common to use a bunch of external services, things like transactional email or logging or working with DB or Cache. In Symphony, you can identify parts of your code and turn those into your own services. Here's a simple service. It represents rolling dice. You can specify how many sides, and it returns a random integer. If I visit my local dev setup, I can re-roll by reloading the page. Now, look at the controller that's responding to that endpoint. Of note, I am type hinting Dice Roll Service on the constructor, but that's it. All I did was create the dice roll service, and Symphony looked at the type hint and grabbed the appropriate object. This works because of auto wiring. If you disable auto wiring and reload, you'll see an error. Too few arguments. When auto wiring is enabled, Symphony gets an instance of the object for you from the service container. For…

Contents