I tried to google it but did not find detailed information.
Service providers are a great way to group related IoC registrations in a single location. Think of them as a way to bootstrap components in your application.
Not understanding from the documentation. Is this only needed when I create packages? So when I am regular developer and not making some packages to release in public - I don't need to care?
So, this is a way to organize your application's services in one place to keep it more organized. A service provider must have at least one method: register. The register method is where the provider binds classes to the container. When a request enters your application and the framework is booting up, the register method is called on the providers listed in your configuration file
This is providers array in
app.php
config file and this is theHtmlServiceProvider
stored in'Illuminate\Html\HtmlServiceProvider.php'
When,
Laravel
boots up, it calls this (register
) method and in this method there are two lines, these line calls two methods,registerHtmlBuilder()
andregisterFormBuilder()
, these both methods components to theIoC
container usingIn this case both are anonymous functions which returns an instance of
html/form
class and that's why, when you useOr, using form
You get the bound class from the
$app
object which is available to your application. In this case'Html' => 'Illuminate\Support\Facades\Html',
is used to alias the main class in thealiases
array in theapp.php
file.So, in
Laravel
, service providers are a way to organize things in a nice cleaner way, during the boot up process of your application,Laravel
runs allregister
methods from all the service providers so each component become available (bound) to theIoC
container so you can access them in your application.It's worth mentioning that, after calling of all
register
methods from service providers all theboot
methods from those service providers get called. In that case, if you need to use any service from the application (IoC/Service Container) within the service provider class then you should use that service from the boot method since it's not guranteed that any service is avaiable during the registeration of service providers (within register method) because services are registered throughregister
method of each service provider but within theboot
method you may use any service because by then every service is hopefully registered.Check this answer Laravel 4 : How are Facades resolved? too, it may help you to understand.
In Laravel, service providers are directly related to the way in which IoC container works. They allow easier and more modular approach to dependencies. In fact, it is a great pattern for organizing your code in a bootstrap fashion ( even outside of Laravel ). I think If you are regular developer you still need to know the basics of Laravel service providers, because that pattern is integral part of Laravel architecture. I am sure it can make your work easier.
For example, when installing a package, your have to give application access to that package - one of the best solution is through service providers list and a facade. On the other hand, I can't imagine being a Laravel developer without knowing basics of SP-s.
There's nothing a service provider can do that you can't just slap into app/start/global.php, but with a service provider you gather all the logic in one place and can develop application in a more modular fashion.
If you're making a package a service provider is pretty much a must.