Laravel Service Container Easy Explanation.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
laravel service container will be easily explained in this video and for this video to be of benefits to you you must have a good understanding of classes all right let's get started now here i have a fresh laravel application and it's returning a view and here is the view i want to do something different i don't want to return a view i want to then dump my application so i'll use the app function then save come here and refresh and this is what i get and this is my entire application this is the laravel application and this laravel application is the same thing as the container so this is what the container is and that's it there's nothing else to add that's what the container is the application is the same thing as the container yeah and now you know what the container is is the application and nothing more so the next thing we're going to look at is how does it work so that we can have a better understanding so how does it work this is how it works you create a service then you store it inside the container here or inside the app it's the same thing after storing it inside here you can be able to retrieve it inside your app and use it so when you store a service into the container where does it go here is where it goes in the bindings and as of now we see we have 65 so we have 65 services so we are going to create our own service inside the and store it in the container then retrieve it in our application and use it in application and by the end of that i believe you'll have a very you'll have a very solid understanding of what service container is so let's do that let's create our own service so first thing i'll do i'll come in my application folder uh create a new file and i'll call it calculate edge i'm going to explain in a few what this does calculate edge class dot php there we go we have a class i'll paste some codes here then i'll explain what is happening so this is our service that we're creating and this service is uh doing a very simple task it's calculating edge so we have a class here called calculated it has a property of edge then it has two functions the first one is provide bathya when somebody provides their birth year assuming they provide 2020 we take 2021 minus that 2020 which is one so this age will be one when we call this function getage we get this age which is one so this service of hours is very simple it is calculating edge we have other complex um services amongst the 65 but i'm using this simple one so that we can understand what happens so now that we've created the service how do we store it in this container of ours or this application of ours the app the container the same thing this is how we store it you come here you say app then you use an inbuilt method of laravel called bind so you say app enter the app i want to bind it's a method and the method takes two arguments the first argument is the key that you want to use for that service so this key you can call it anything you want i'll call minecraft calculator it's calculator then a comma the second argument is a function function [Applause] it's an anonymous function and it returns an instance of that service so return an instance um new up then calculated class an instant sorry an instance uh let me terminate this and there we go simple as that now we've been we've uh bind this service to the service container watch this now we have 65 services let's refresh and see and now we have 66 now we've stored a function inside the container let's see if it's here we have a lot of services here let's scroll down let's go let's go let's scroll ah what's the name of our edge calculator here it is edge calculator it's now stored and it's stored using the key you see is the key i assuming now you wanted to see if that that that service is stored there is an inbuilt method of laravel called resolve so you can come you can just come here and say dye and dump resolve the method then here you pass the key of your service of your service and our key was edge calculate calculator so let's copy that let's paste it here and let's save let's refresh and there we go age calculator that's how we resolve it so now we have it stored inside the container how can we use it in our application how can we use that service very simple you just come here and see the function then you you take the name of your service the name was calculated class you just write it here calculate edge class then [Music] you store it in a variable you can call this variable anything i'll call mine the same i think that it will be easy to understand i'll call it calculate edge but you can use any name that you want you can use any name that you want okay let me expand this a little bit that's good so we've um we've imported that class and we've stored it in a variable now the laravel service container will automatically inject it here and now we can use this edge calculate each class inside here let's see if we can so let's take this method sorry this variable which is storing this service then this variable or this class this is the variable that is containing this class let's see the method that you can use which we have provide birth yeah let's use it let's come here and let's see method it has this provide there it's even there you're even being uh given hints that it is provided back here let's say somebody was born in um 20 2010 comma then we can come here and say so the age of the person [Applause] the age the age of the person should be equal to let's take this one calculate edge class then it has the method of getting the edge uh what's the method get edge so let's come here and say get age there it is and then now we can return the edge we can return the edge let me go through this once more before we see the results we created a service we created a service here it is this is our service it's doing something it's calculating edge we came here we bind it to the service container and by binding it we use the bind method we provide a key any name that you want then the second argument is a function and that function returns a new instance of that service here it is now this is a service that we've created so we can use it anywhere so like here if you want to use it we just come here and we say we call the name of the service which was calculated class it is we store it in a variable and now we can use the methods that are inside the calculated class and that's how laravel works it has a lot of services that have the methods that you use like when you use request object uh for requesting the inputs from a form is this is the same thing it's the same thing where you'll see they'll uh they'll use a request request with the same thing so here we use those methods provide path here 2010 then we say age is equal to this we use the same method that's there get age then we return age let's save let's see what we get and you have a a problem target class up calculate each class does not exist calculate edge class does not exist let's try and see what the problem is i've calculated each class does not exist i think this is the problem here it should not be a small a it should be a capital a yeah and there we go it's 11 years we can try something different let's different let's say somebody let's say somebody who was born in 20 15 save very fresh and there we go that guy is six years 2020 from 2020 2015 to 2021 that is six years and that's what's laravel service container and that's how it works simple nothing complicated thank you for watching if this video has been our help to you kindly leave a comment leave a like and please subscribe see you next time
Info
Channel: Alphayo Wakarindi
Views: 3,414
Rating: undefined out of 5
Keywords: laravel, service container, laravel 8
Id: 5glsdzGeYQo
Channel Id: undefined
Length: 12min 24sec (744 seconds)
Published: Sun May 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.