Service Providers in Laravel: What They Are and How to Use Them

Tutorials

April 22nd, 2022

Service Providers in Laravel: What They Are and How to Use Them

For those who haven't actively used Service Providers in Laravel, it's a mystical "term": what "service" do they actually "provide", and how exactly does it all work? I will explain it in this article.


Default Laravel Service Providers

Let's start with the default service providers included in Laravel, they are all in the app/Providers folder:

  • AppServiceProvider
  • AuthServiceProvider
  • BroadcastServiceProvider
  • EventServiceProvider
  • RouteServiceProvider

They are all PHP classes, each related to its topic: general "app", Auth, Broadcasting, Events, and Routes. But they all have one thing in common: the boot() method.

Inside of that method, you can write any code related to one of those sections: auth, events, routes, etc. In other words, Service Providers are just classes to register some global functionality.

They are separated as "providers" because they are executed really early in the Application Lifecycle, so it is convenient something global here before the execution script comes to Models or Controllers.

The most amount of functionality is in the RouteServiceProvider, let's take a look at its code:

1class RouteServiceProvider extends ServiceProvider
2{
3 public const HOME = '/dashboard';
4 
5 public function boot()
6 {
7 $this->configureRateLimiting();
8 
9 $this->routes(function () {
10 Route::prefix('api')
11 ->middleware('api')
12 ->group(base_path('routes/api.php'));
13 
14 Route::middleware('web')
15 ->group(base_path('routes/web.php'));
16 });
17 }
18 
19 protected function configureRateLimiting()
20 {
21 RateLimiter::for('api', function (Request $request) {
22 return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
23 });
24 }
25}

This is the class where route files are configured, with routes/web.php and routes/api.php included by default. Notice that for the API there are also different configurations: endpoint prefix /api and middleware api for all the routes.

You can edit those Service Providers however you want, they are not in the /vendor folder. Typical customization of this file happens when you have a lot of routes and you want to separate them in your custom file. You create routes/auth.php and put the routes there, and then you "enable" that file in the boot() method of the RouteServiceProvider, just add the third sentence:

1`Route::middleware('web') // or maybe you want another middleware?
2 ->group(base_path('routes/auth.php'));

Other default service providers have other functionality, you can analyze them by yourself. Except for AppServiceProvider, it is empty, like a placeholder for us to add any code related to some global application settings.

One popular example of adding code to the AppServiceProvider is about disabling the lazy loading in Eloquent. To do that, you just need to add two lines into the boot() method:

1// app/Providers/AppServiceProvider.php
2use Illuminate\Database\Eloquent\Model;
3 
4public function boot()
5{
6 Model::preventLazyLoading(! $this->app->isProduction());
7}

This will throw an exception if some relationship model isn't eager loaded, which causes a so-called N+1 query problem with performance.


When Service Providers Are Executed?

If you look at the official docs about request lifecycle, these are the things executed in the very beginning:

  • public/index.php
  • bootstrap/app.php
  • app/Http/Kernel.php and its Middlewares
  • Service Providers: exactly our topic of this article

Which providers are loaded? It's defined in the config/app.php array:

1return [
2 
3 // ... other configuration values
4 
5 'providers' => [
6 
7 /*
8 * Laravel Framework Service Providers...
9 */
10 Illuminate\Auth\AuthServiceProvider::class,
11 Illuminate\Broadcasting\BroadcastServiceProvider::class,
12 
13 // ... other framework providers from /vendor
14 Illuminate\Validation\ValidationServiceProvider::class,
15 Illuminate\View\ViewServiceProvider::class,
16 
17 /*
18 * PUBLIC Service Providers - the ones we mentioned above
19 */
20 App\Providers\AppServiceProvider::class,
21 App\Providers\AuthServiceProvider::class,
22 // App\Providers\BroadcastServiceProvider::class,
23 App\Providers\EventServiceProvider::class,
24 App\Providers\RouteServiceProvider::class,
25 
26 ],
27 
28];

As you can see, there's a list of non-public service providers from the /vendor folder, you shouldn't touch/edit them. The ones we're interested in are at the bottom, with BroadcastServicerProvider disabled by default, probably because it's rarely used.

All of those service providers are executed from top to bottom, iterating that list twice.

The first iteration is looking for an optional method register() which may be used to initiate something before the boot() method. I've never used it in my experience.

Then, the second iteration executes the boot() method of all providers. Again, one by one, from top to bottom of the 'providers' array.

And then, after all the service providers have been processed, Laravel goes to parsing the route, executing the Controller, using Models, etc.


Create Your Custom Service Provider

In addition to the existing default files, you can easily create your service provider, related to some other topics than the default ones like auth/event/routes.

Quite a typical example is configuration related to Blade views. If you want to create your Blade directive, you can add that code into any service provider's boot() method, including the default AppServiceProvider, but quite often developers create a separate ViewServiceProvider.

You can generate it with this command:

1php artisan make:provider ViewServiceProvider

It will generate the default template:

1namespace App\Providers;
2 
3use Illuminate\Support\ServiceProvider;
4 
5class ViewServiceProvider extends ServiceProvider
6{
7 /**
8 * Register services.
9 *
10 * @return void
11 */
12 public function register()
13 {
14 //
15 }
16 
17 /**
18 * Bootstrap services.
19 *
20 * @return void
21 */
22 public function boot()
23 {
24 //
25 }
26}

You may remove the register() method, and inside of boot() add Blade directive code:

1use Illuminate\Support\Facades\Blade;
2 
3public function boot()
4{
5 Blade::directive('datetime', function ($expression) {
6 return "<?php echo ($expression)->format('m/d/Y H:i'); ?>";
7 });
8}

Another example of a ViewServiceProvider is about View Composers, here's the snippet from the official Laravel docs:

1use App\View\Composers\ProfileComposer;
2use Illuminate\Support\Facades\View;
3use Illuminate\Support\ServiceProvider;
4 
5class ViewServiceProvider extends ServiceProvider
6{
7 public function boot()
8 {
9 // Using class based composers...
10 View::composer('profile', ProfileComposer::class);
11 
12 // Using closure based composers...
13 View::composer('dashboard', function ($view) {
14 //
15 });
16 }
17}

To be executed, this new provider should be added to the array of providers in config/app.php, as mentioned above:

1return [
2 // ... other configuration values
3 
4 'providers' => [
5 
6 App\Providers\AppServiceProvider::class,
7 App\Providers\AuthServiceProvider::class,
8 // App\Providers\BroadcastServiceProvider::class,
9 App\Providers\EventServiceProvider::class,
10 App\Providers\RouteServiceProvider::class,
11 
12 // Add your provider here
13 App\Providers\ViewServiceProvider::class,
14 ],
15];

Examples from Open-Source Projects

Finally, I want to mention a few examples from freely available Laravel projects.

1. spatie/freek.dev: BladeComponentServiceProvider

A well-known company Spatie has published the source code for the personal blog of Freek Van der Herten, with this file.

app/Providers/BladeComponentServiceProvider.php:

1namespace App\Providers;
2 
3use App\Http\Components\AdComponent;
4use Illuminate\Support\Facades\Blade;
5use Illuminate\Support\ServiceProvider;
6 
7class BladeComponentServiceProvider extends ServiceProvider
8{
9 public function boot()
10 {
11 Blade::component('ad', AdComponent::class);
12 
13 Blade::component('front.components.inputField', 'input-field');
14 Blade::component('front.components.submitButton', 'submit-button');
15 Blade::component('front.components.textarea', 'textarea');
16 Blade::component('front.components.textarea', 'textarea');
17 Blade::component('front.components.shareButton', 'share-button');
18 Blade::component('front.components.lazy', 'lazy');
19 Blade::component('front.components.postHeader', 'post-header');
20 
21 Blade::component('front.layouts.app', 'app-layout');
22 }
23}

View source on Github

2. monicahq/monica: MacroServiceProvider

One of the most-starred Laravel open-source projects has a separate file to register Collection macros:

app/Providers/MacroServiceProvider.php:

1namespace App\Providers;
2 
3use App\Helpers\CollectionHelper;
4use Illuminate\Support\Collection;
5use Illuminate\Support\ServiceProvider;
6 
7class MacroServiceProvider extends ServiceProvider
8{
9 /**
10 * Bootstrap any application services.
11 *
12 * @return void
13 */
14 public function boot()
15 {
16 if (! Collection::hasMacro('sortByCollator')) {
17 Collection::macro('sortByCollator', function ($callback, $options = \Collator::SORT_STRING, $descending = false) {
18 /** @var Collection */
19 $collect = $this;
20 
21 return CollectionHelper::sortByCollator($collect, $callback, $options, $descending);
22 });
23 }
24 
25 if (! Collection::hasMacro('groupByItemsProperty')) {
26 Collection::macro('groupByItemsProperty', function ($property) {
27 /** @var Collection */
28 $collect = $this;
29 
30 return CollectionHelper::groupByItemsProperty($collect, $property);
31 });
32 }
33 
34 if (! Collection::hasMacro('mapUuid')) {
35 Collection::macro('mapUuid', function () {
36 /** @var Collection */
37 $collect = $this;
38 
39 return $collect->map(function ($item) {
40 return $item->uuid;
41 })->toArray();
42 });
43 }
44 }
45}

View source on Github

3. phpreel/phpreelcms: DashboardComponentsServiceProvider

Laravel CMS called phpReel also has a service provider for Blade components, named even longer.

app/Providers/DashboardComponentsServiceProvider.php:

1 
2namespace App\Providers;
3 
4use Illuminate\Support\ServiceProvider;
5use Illuminate\Support\Facades\Blade;
6use App\Helpers\FileUpload\UploadComponents;
7 
8class DashboardComponentsServiceProvider extends ServiceProvider
9{
10 /**
11 * Register services.
12 *
13 * @return void
14 */
15 public function register()
16 {
17 //
18 }
19 
20 /**
21 * Bootstrap services.
22 *
23 * @return void
24 */
25 public function boot()
26 {
27 Blade::directive('uploadForm', function () {
28 $component = UploadComponents::getUploadForm();
29 $html = '<?php echo \'' . $component . '\'; ?>';
30 
31 return ('<?php echo "' . $component . '"; ?>');
32 });
33 }
34}

View source on Github

You can also find a few more examples of Service Providers at my LaravelExamples.com website.

Filed in:

PovilasKorop

A web-developer with 15+ years experience, founder of Laravel QuickAdminPanel generator.

Sharing Laravel lessons on Youtube with channel Laravel Daily.