Рет қаралды 10,560
In today's lesson, we will touch on an important and interesting topic - service container (laravel service container). This is an architectural concept in Laravel that is important to know and understand, but for many newbies it can be a dark forest.
serviceconteiner # laravel # cutcode
❗️❗️❗️How to make complex sites in laravel? It's easy with cutcode!
Support my project - cutcode.ru/
Buy me coffee - buymeacoffee.com/cutcode
🤖🤖🤖My assistant Taylor is ready to give you a present. Pick up here - cutcode.ru/chat-bot
-------------------------------------------------- -------------------------------
⏰ Timecodes:
00:00 Introduction
00:27 Laravel Documentation Overview
02:00 A little story about how I studied service container many years ago
02:58 Class injection example
04:48 What Happens When Specifying an Argument Type
06:01 An example with interface injection
Friends, I welcome everyone to the Cutcode channel! In today's lesson, we will touch on an important and interesting topic - service container (laravel service container). This is an architectural concept in Laravel that is important to know and understand, but for many newbies it can be a dark forest.
The Laravel documentation describes it in detail, but there is a lot of complicated, incomprehensible text. Therefore, we will now try to illuminate it with an example in simple words, so that everything becomes immediately clear and in the future, when you hear such a phrase as a service container, you immediately understand what it is about and how it all works, all this magic.
In the documentation, we immediately see a simple example, we see the service container controller working for us as described in the documentation in controllers, in event listeners, middleware and elsewhere. But I'll tell you right away in 90 percent of cases, or even more, this is only used in controllers. Therefore, in today's lesson we will just consider controllers.
We return to the documentation where a simple example is shown to us. The example controller user has a construct method. It can also be any other method in this controller. Well, in any other Laravel controllers.
We then pass the argument to the users function specifying the type of the users class of the repository. At the same time, it is in Laravel that this service container magic happens and we create an instance of the user class of the repository with which we can already work inside this method. We can also pass any of our classes and can pass and inject multiple classes, specifying them separated by commas. Moving a little from the documentation, I will say that when I just started learning Laravel and it was version 6 or even 5, and by default, when generating a controller via the command line, there was a default index method and it had just an injection of the request facade. And I thought that by default only the request facade, or request forms, or some parameters from the route are available to us, and only later I delved into the Laravel concept and realized how interesting it all works.
In addition to class injection, we can also inject interfaces and later bind them to some class that inherits this interface. But we will talk about this now in more detail in the examples, so that it becomes clearer when and why such an approach should be applied.
Let's open up the code and walk through live examples.
-------------------------------------------------- -------------------------------
📹 share this video with your friends:
• Service container в La...
🔔 subscribe to the KZbin channel: / @cutcoderu
📼 Laravel course from scratch:
• Курс по Laravel 8 обуч...
Service conteiner in Laravel - architectural concept and injection in Laravel
-------------------------------------------------- -------------------------------
🔗 our website: cutcode.ru/
📷 our instagram: / cutcoderu