Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 - when to use service providers?

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?

like image 734
Dariux Avatar asked Dec 07 '13 09:12

Dariux


People also ask

Why do we need service provider in Laravel?

Service providers can be defined as the central place to configure all the entire Laravel applications. Applications, as well as Laravel's core services, are bootstrapped via service providers. These are powerful tools for maintaining class dependencies and performing dependency injection.

What is the use of service provider?

A service provider is a vendor that provides IT solutions and/or services to end users and organizations. This broad term incorporates all IT businesses that provide products and solutions through services that are on-demand, pay per use or a hybrid delivery model.

What is the difference between service provider and service container in Laravel?

Service container is the place our application bindings are stored. And the service providers are the classes where we register our bindings to service container. In older releases of Laravel, we didn't have these providers and people were always asking where to put the bindings.

What is the purpose of register and boot methods in the service provider class?

Providers have two lifecycle methods: register and boot . The register method is where you should add bindings to the service container. The boot method is for performing actions after all service providers have registered their services.


2 Answers

One of the keys to building a well architected Laravel application is learning to use serviceproviders as an organizational tool. When you are registering many classes with the IoC container, all of those bindings can start to clutter your app/start files. Instead of doing container registrations in those files, create serviceproviders that register related services.

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

'providers' => array(     'Illuminate\Foundation\Providers\ArtisanServiceProvider',     'Illuminate\Auth\AuthServiceProvider',     'Illuminate\Cache\CacheServiceProvider',     // more ...     'Illuminate\Html\HtmlServiceProvider',     // more ... ) 

This is providers array in app.php config file and this is the HtmlServiceProvider stored in 'Illuminate\Html\HtmlServiceProvider.php'

use Illuminate\Support\ServiceProvider;     class HtmlServiceProvider extends ServiceProvider {      //...     public function register()     {         $this->registerHtmlBuilder();          $this->registerFormBuilder();     }      protected function registerHtmlBuilder()     {         $this->app['html'] = $this->app->share(function($app)         {             return new HtmlBuilder($app['url']);         });     }      protected function registerFormBuilder()     {         $this->app['form'] = $this->app->share(function($app)         {             $form = new FormBuilder($app['html'], $app['url'], $app['session']->getToken());             return $form->setSessionStore($app['session']);         });     }  } 

When, Laravel boots up, it calls this (register) method and in this method there are two lines, these line calls two methods, registerHtmlBuilder() and registerFormBuilder(), these both methods components to the IoC container using

$this->app['html'] = $this->app->share(...); $this->app['form'] = $this->app->share(...); 

In this case both are anonymous functions which returns an instance of html/form class and that's why, when you use

Html::link(...); 

Or, using form

Form::input(...); 

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 the aliases array in the app.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 all register methods from all the service providers so each component become available (bound) to the IoC container so you can access them in your application.

It's worth mentioning that, after calling of all register methods from service providers all the boot 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 through register method of each service provider but within the boot 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.

like image 108
The Alpha Avatar answered Sep 19 '22 19:09

The Alpha


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.

like image 33
Andreas Avatar answered Sep 17 '22 19:09

Andreas