Laravel Service Providers: All You Need to Know

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today i want to talk about probably one of the most misunderstood concepts in laravel and these are service providers it is in the documentation it is kind of explained but without too much practice so as i'm doing often in these videos on this channel i want to take it practical so what are service provider how do you use them inside of that laravel default structure how do you create your own service providers what should be the methods inside and most important what are the examples not of how to use providers but why you should use them in what cases let's dive in and by default in laravel here's a totally fresh laravel 8 project there is a folder app providers with 5 classes of providers and they are all registered in the file config app in the array called providers after the vendor providers there are five providers which are public which you can edit however you want one of them is commented out so we will not talk about that in this lesson but probably the most important is app service provider and it has two methods register and boot both empty so it's kind of prepared for you to fill in with your data and what should you fill in here if you go to laravel documentation and search for service providers there are quite a few examples of registering something or changing some config in app service provider because app service provider and actually all service providers i like config classes but just not in config in array format so for example in the config you can open config app again and it's an array so providers aliases and stuff like that and in service provider you can configure something in object-oriented programming way so define something for some class which is already available so use facades use whatever code you want to configure something but outside of that config array type of configuration okay enough theory i have six most typical examples of how to use app service provider first example is configuring the pagination so what views or what styles to choose for the pagination on the whole laravel project you cannot configure that in config files so there is no config pagination.php file but you can do that in app service provider and it was particularly important with laravel 8 release when they changed the bootstrap to tailwind by default and if you wanted to override it back so for example using bootstrap it's a separate section in the documentation in app service provider in boot method you would need to specify that paginator should use bootstrap or if you have your own views for pagination same here app service provider boot you can provide your own view so pagination would work in your way next example is creating your own blade directive so for example you want to have add something like at date time in this example and guess where you register that in app service provider in the boot method you just go blade directive and inside of that what to return next example is api resources and they return json format with data by default but you can disable that again where in app service provider and here if you scroll down app service provider you can specify json resource without wrapping so again one line of code it's kind of like a configuration but not in config api file or something it's in app service provider next example will take us outside of app service provider into another default service provider model observers for eloquent to register that observer so you have that class and if we scroll down the documentation there's user observe and you define that in the boot method of event service provider class so why event service provider are not app service provider to be honest the only difference between them is just the purpose of the class the naming of the class where it is actually used so app is for all application and things that are topical for routes for events or for anything else or other service providers that's the only difference so you can specify and you can create your own service provider like view service provider which isn't there by default and we will get to that in a minute but technically it all could be an app service provider without those five different classes and that would still work of course there's a slight difference in when they are called so in config app there's a specific order in which they are called so app service provider is the first and then those topical service providers as i call them are a bit later so in theory if we change the order it may break something and we shouldn't do that but i wanted to explain what is the service provider prefix is just where it belongs so it could be whatever service provider is just that laravel by default gives these ones and as laravel documentation says in all the times about service provider you may register something so there's no strict rule this could be done in app service provider in the event service provider in your custom service provider wherever the main thing is that it should be boot method and remember there are two methods in service providers register and boot and all the examples before that are in boot method and that's the one you should know register in most cases you don't need to change or create that register and we will get to that example of register in a minute but in 99 of the cases you need to know about boot method only another example is sharing global data with all the views and this is a pretty typical thing in laravel projects again we're touching app service provider where you can do things like this view share so you can create a variable key assign the value which will be accessible in all the views throughout all the project so that's one way of doing that but if you scroll a bit down you can create a view composer class which would contain more logic about your variables and then where do you register that class and this is the key paragraph i will read it for you typically again that's typically there's no rule view composers will be registered within one of your service providers documentation doesn't say which one it could be app service provider or in this example we'll assume that we have created a new view service provider remember it doesn't come by default from laravel you can create that and there's an artisan command for that php artisan make provider view service provider and then inside of that service provider you would fill boot method with whatever composer files you want to use so when would you want to create your own service provider and again we refer to another section of laravel documentation which by the way was rewritten a few months ago by taylor so if you haven't read laravel documentation in like six months or so i totally recommend to do that again because a lot of that were rewritten in more human language so to speak so one of those examples is request lifecycle there is a focus on service providers and one sentence is important by default the app service provider is fairly empty and for large applications you may wish to again you may you may wish to create several service providers each with more granular bootstrapping for specific services so this is not that much human language but generally if you have a bigger application and you want to have more logic specifically to some section of your application like view composers for example you may but not must you may create a view service provider if you don't want to do that you can do all the logic in app service provider by default now let's briefly take a look at other service providers provided by laravel by default what's inside of them and what can we customize there so odd service provider has an example policy registration and all it does is register policies by default in most cases you don't need to change anything here let's go further event service provider in the event service provider you may register your own listeners and events in listen array so you don't need to change boot method at all it is empty but it has the protected property listed and route service provider this is more interesting because in the route it's a boot method with a lot of logic so configure rate limiting which is just configure that is 60 attempts max per minute for api by default and then it registers routes so two files routes web php and routes api php are registered in the route service provider and if you want your own routes admin for example php you should add a third line here and if we search all laravel documentation for those service providers for example so auth service provider i've put that in the search in the github docs and that's actually a better way to search laravel docs if you want more results so if you load all service provider in the default level documentation here it will show only five results and not that clear the context so if you want to dig deeper you can search with laravel docs on github so slash laravel docs and then use that search so for auth service provider the examples i found is registering custom guard in auth service provider then specifying the verification email details for verifying the email of a new user and then specifying the url for password reset so again it's a proof that service providers are kind of like configuration files but in more object-oriented programming way with using laravel facades and a few more examples with route service provider i found so you can specify the pattern that every id parameter in your url will specify the rule of that it is a number specifically so there's a global rule or another example for route model binding you can specify that user is always a user class and another example with route service provider is you can customize the create edit and other urls for resource controllers so in the url instead of user slash create for example you can create it in a different language or in totally different verb so the visual part the method stays the same in the controller so method create but visually for example if your whole website is in another language visually people would see create for example it is for spanish i think now let's briefly talk about register method as i told you in most cases you don't need that and in default app service provider and other providers by laravel it doesn't exist but if you want to register something that may be used in other service providers like a global global thing you may use register method and just register something here like some class bind to something and the whole idea that providers are loaded with register methods one by one and then boot methods so boot of one provider boot of another provider and boot a third provider so if in your boot method you want to use something that is registered by other provider then that other provider should give that in register method not in boot it's a bit complex but i hope it makes sense and a few examples of that is one of the repository that i've reviewed previously on this channel open source send portal and they have their own service provider for send portal app and in the register they just bind the repositories to the interface which repository class to use with which interface in what condition but generally the examples that i've seen for register method is just binding the classes and boot method is mostly for configuration of the application parts so again in most cases you don't need to touch register unless you're using some design pattern that requires that maybe more understandable example of register method is telescope so laravel telescope when installing that you need to provide the register method with this so you should not use telescope service provider as a provider in all the application because telescope needs to run only locally so it should not be run on production server and this is the way how you do that so if the environment is local then you register the classes that are used by telescope and final note where service providers should be used really in depth is when you're creating your own packages so in all this video we've discussed how to use that in the laravel application but if you are creating your own package then service providers are really important for you so in your package as composer json you need to provide your providers and if we take a look at the example of laravel debug bar service provider i've opened that in the github this is how it looks so it has a lot more logic data loading something binding something registering the commands registering macros a lot of stuff so part of that is in the boot part of that is in the register so if you are creating your own packages or want to create them then read about service providers a lot more but this is outside of the topic of this video in this video i wanted to explain what our service providers how to use them how to configure them and kind of translate laravel docs into more human practical language with examples i hope it's helpful and if you want more videos on this channel explaining stuff in more practical ways i'm trying to do that daily the channel is called laravel daily and you can support that by financing one of the three products that you can see on the screen it's not a donation you still get values from your product and the more money our team gets from those products the more time i personally have to shoot these videos like this one see you guys in other videos
Info
Channel: Laravel Daily
Views: 40,170
Rating: undefined out of 5
Keywords:
Id: VYPfncvYW-Y
Channel Id: undefined
Length: 13min 13sec (793 seconds)
Published: Sat Apr 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.