ā¹ļø Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | FAIL | meta_canonical IS NULL OR = '' OR = src_unparsed | com,laravel!/docs/13.x/providers s443 |
| Property | Value |
|---|---|
| URL | https://laravel.com/docs/12.x/providers |
| Last Crawled | 2026-04-15 23:11:09 (8 hours ago) |
| First Indexed | 2025-02-24 16:47:35 (1 year ago) |
| HTTP Status Code | 200 |
| Meta Title | Service Providers | Laravel 12.x - The clean stack for Artisans and agents |
| Meta Description | Laravel is a PHP web application framework with expressive, elegant syntax. We've already laid the foundation ā freeing you to create without sweating the small things. |
| Meta Canonical | com,laravel!/docs/13.x/providers s443 |
| Boilerpipe Text | WARNING
You're browsing the documentation for an old version of Laravel. Consider upgrading your project to
Laravel 13.x
.
Introduction
Writing Service Providers
The Register Method
The Boot Method
Registering Providers
Deferred Providers
Introduction
Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services, are bootstrapped via service providers.
But, what do we mean by "bootstrapped"? In general, we mean
registering
things, including registering service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application.
Laravel uses dozens of service providers internally to bootstrap its core services, such as the mailer, queue, cache, and others. Many of these providers are "deferred" providers, meaning they will not be loaded on every request, but only when the services they provide are actually needed.
All user-defined service providers are registered in the
bootstrap/providers.php
file. In the following documentation, you will learn how to write your own service providers and register them with your Laravel application.
If you would like to learn more about how Laravel handles requests and works internally, check out our documentation on the Laravel
request lifecycle
.
Writing Service Providers
All service providers extend the
Illuminate\Support\ServiceProvider
class. Most service providers contain a
register
and a
boot
method. Within the
register
method, you should
only bind things into the
service container
. You should never attempt to register any event listeners, routes, or any other piece of functionality within the
register
method.
The Artisan CLI can generate a new provider via the
make:provider
command. Laravel will automatically register your new provider in your application's
bootstrap/providers.php
file:
1
php
artisan
make:provider
RiakServiceProvider
The Register Method
As mentioned previously, within the
register
method, you should only bind things into the
service container
. You should never attempt to register any event listeners, routes, or any other piece of functionality within the
register
method. Otherwise, you may accidentally use a service that is provided by a service provider which has not loaded yet.
Let's take a look at a basic service provider. Within any of your service provider methods, you always have access to the
$app
property which provides access to the service container:
1
<?php
2
3
namespace
App\Providers;
4
5
use
App\Services\Riak\
Connection
;
6
use
Illuminate\Contracts\Foundation\
Application
;
7
use
Illuminate\Support\
ServiceProvider
;
8
9
class
RiakServiceProvider
extends
ServiceProvider
10
{
11
/**
12
* Register any application services.
13
*/
14
public
function
register
()
:
void
15
{
16
$this
->app->
singleton
(
Connection
::
class
,
function
(
Application
$app
)
{
17
return
new
Connection
(
config
(
'
riak
'
));
18
});
19
}
20
}
This service provider only defines a
register
method, and uses that method to define an implementation of
App\Services\Riak\Connection
in the service container. If you're not yet familiar with Laravel's service container, check out
its documentation
.
The
bindings
and
singletons
Properties
If your service provider registers many simple bindings, you may wish to use the
bindings
and
singletons
properties instead of manually registering each container binding. When the service provider is loaded by the framework, it will automatically check for these properties and register their bindings:
1
<?php
2
3
namespace
App\Providers;
4
5
use
App\Contracts\
DowntimeNotifier
;
6
use
App\Contracts\
ServerProvider
;
7
use
App\Services\
DigitalOceanServerProvider
;
8
use
App\Services\
PingdomDowntimeNotifier
;
9
use
App\Services\
ServerToolsProvider
;
10
use
Illuminate\Support\
ServiceProvider
;
11
12
class
AppServiceProvider
extends
ServiceProvider
13
{
14
/**
15
* All of the container bindings that should be registered.
16
*
17
*
@var
array
18
*/
19
public
$bindings
=
[
20
ServerProvider
::
class
=>
DigitalOceanServerProvider
::
class
,
21
];
22
23
/**
24
* All of the container singletons that should be registered.
25
*
26
*
@var
array
27
*/
28
public
$singletons
=
[
29
DowntimeNotifier
::
class
=>
PingdomDowntimeNotifier
::
class
,
30
ServerProvider
::
class
=>
ServerToolsProvider
::
class
,
31
];
32
}
The Boot Method
So, what if we need to register a
view composer
within our service provider? This should be done within the
boot
method.
This method is called after all other service providers have been registered
, meaning you have access to all other services that have been registered by the framework:
1
<?php
2
3
namespace
App\Providers;
4
5
use
Illuminate\Support\Facades\
View
;
6
use
Illuminate\Support\
ServiceProvider
;
7
8
class
ComposerServiceProvider
extends
ServiceProvider
9
{
10
/**
11
* Bootstrap any application services.
12
*/
13
public
function
boot
()
:
void
14
{
15
View
::
composer
(
'
view
'
,
function
()
{
16
//
...
17
});
18
}
19
}
Boot Method Dependency Injection
You may type-hint dependencies for your service provider's
boot
method. The
service container
will automatically inject any dependencies you need:
1
use
Illuminate\Contracts\Routing\
ResponseFactory
;
2
3
/**
4
* Bootstrap any application services.
5
*/
6
public
function
boot
(
ResponseFactory
$response
)
:
void
7
{
8
$response
->
macro
(
'
serialized
'
,
function
(
mixed
$value
)
{
9
//
...
10
});
11
}
Registering Providers
All service providers are registered in the
bootstrap/providers.php
configuration file. This file returns an array that contains the class names of your application's service providers:
1
<?php
2
3
return
[
4
App\Providers\
AppServiceProvider
::
class
,
5
];
When you invoke the
make:provider
Artisan command, Laravel will automatically add the generated provider to the
bootstrap/providers.php
file. However, if you have manually created the provider class, you should manually add the provider class to the array:
1
<?php
2
3
return
[
4
App\Providers\
AppServiceProvider
::
class
,
5
App\Providers\
ComposerServiceProvider
::
class
,
6
];
Deferred Providers
If your provider is
only
registering bindings in the
service container
, you may choose to defer its registration until one of the registered bindings is actually needed. Deferring the loading of such a provider will improve the performance of your application, since it is not loaded from the filesystem on every request.
Laravel compiles and stores a list of all of the services supplied by deferred service providers, along with the name of its service provider class. Then, only when you attempt to resolve one of these services does Laravel load the service provider.
To defer the loading of a provider, implement the
\Illuminate\Contracts\Support\DeferrableProvider
interface and define a
provides
method. The
provides
method should return the service container bindings registered by the provider:
1
<?php
2
3
namespace
App\Providers;
4
5
use
App\Services\Riak\
Connection
;
6
use
Illuminate\Contracts\Foundation\
Application
;
7
use
Illuminate\Contracts\Support\
DeferrableProvider
;
8
use
Illuminate\Support\
ServiceProvider
;
9
10
class
RiakServiceProvider
extends
ServiceProvider
implements
DeferrableProvider
11
{
12
/**
13
* Register any application services.
14
*/
15
public
function
register
()
:
void
16
{
17
$this
->app->
singleton
(
Connection
::
class
,
function
(
Application
$app
)
{
18
return
new
Connection
(
$app
[
'
config
'
][
'
riak
'
]);
19
});
20
}
21
22
/**
23
* Get the services provided by the provider.
24
*
25
*
@return
array
<
int
, string>
26
*/
27
public
function
provides
()
:
array
28
{
29
return
[
Connection
::
class
];
30
}
31
} |
| Markdown | - [Home](https://laravel.com/)
Search
```K`
āK
Switch to light mode
[Skip to content](https://laravel.com/docs/12.x/providers#main-content)
- ## Prologue
- [Release Notes](https://laravel.com/docs/12.x/releases)
- [Upgrade Guide](https://laravel.com/docs/12.x/upgrade)
- [Contribution Guide](https://laravel.com/docs/12.x/contributions)
- ## Getting Started
- [Installation](https://laravel.com/docs/12.x/installation)
- [Configuration](https://laravel.com/docs/12.x/configuration)
- [Agentic Development](https://laravel.com/docs/12.x/ai)
- [Directory Structure](https://laravel.com/docs/12.x/structure)
- [Frontend](https://laravel.com/docs/12.x/frontend)
- [Starter Kits](https://laravel.com/docs/12.x/starter-kits)
- [Deployment](https://laravel.com/docs/12.x/deployment)
- ## Architecture Concepts
- [Request Lifecycle](https://laravel.com/docs/12.x/lifecycle)
- [Service Container](https://laravel.com/docs/12.x/container)
- [Service Providers](https://laravel.com/docs/12.x/providers)
- [Facades](https://laravel.com/docs/12.x/facades)
- ## The Basics
- [Routing](https://laravel.com/docs/12.x/routing)
- [Middleware](https://laravel.com/docs/12.x/middleware)
- [CSRF Protection](https://laravel.com/docs/12.x/csrf)
- [Controllers](https://laravel.com/docs/12.x/controllers)
- [Requests](https://laravel.com/docs/12.x/requests)
- [Responses](https://laravel.com/docs/12.x/responses)
- [Views](https://laravel.com/docs/12.x/views)
- [Blade Templates](https://laravel.com/docs/12.x/blade)
- [Asset Bundling](https://laravel.com/docs/12.x/vite)
- [URL Generation](https://laravel.com/docs/12.x/urls)
- [Session](https://laravel.com/docs/12.x/session)
- [Validation](https://laravel.com/docs/12.x/validation)
- [Error Handling](https://laravel.com/docs/12.x/errors)
- [Logging](https://laravel.com/docs/12.x/logging)
- ## Digging Deeper
- [Artisan Console](https://laravel.com/docs/12.x/artisan)
- [Broadcasting](https://laravel.com/docs/12.x/broadcasting)
- [Cache](https://laravel.com/docs/12.x/cache)
- [Collections](https://laravel.com/docs/12.x/collections)
- [Concurrency](https://laravel.com/docs/12.x/concurrency)
- [Context](https://laravel.com/docs/12.x/context)
- [Contracts](https://laravel.com/docs/12.x/contracts)
- [Events](https://laravel.com/docs/12.x/events)
- [File Storage](https://laravel.com/docs/12.x/filesystem)
- [Helpers](https://laravel.com/docs/12.x/helpers)
- [HTTP Client](https://laravel.com/docs/12.x/http-client)
- [Localization](https://laravel.com/docs/12.x/localization)
- [Mail](https://laravel.com/docs/12.x/mail)
- [Notifications](https://laravel.com/docs/12.x/notifications)
- [Package Development](https://laravel.com/docs/12.x/packages)
- [Processes](https://laravel.com/docs/12.x/processes)
- [Queues](https://laravel.com/docs/12.x/queues)
- [Rate Limiting](https://laravel.com/docs/12.x/rate-limiting)
- [Search](https://laravel.com/docs/12.x/search)
- [Strings](https://laravel.com/docs/12.x/strings)
- [Task Scheduling](https://laravel.com/docs/12.x/scheduling)
- ## Security
- [Authentication](https://laravel.com/docs/12.x/authentication)
- [Authorization](https://laravel.com/docs/12.x/authorization)
- [Email Verification](https://laravel.com/docs/12.x/verification)
- [Encryption](https://laravel.com/docs/12.x/encryption)
- [Hashing](https://laravel.com/docs/12.x/hashing)
- [Password Reset](https://laravel.com/docs/12.x/passwords)
- ## Database
- [Getting Started](https://laravel.com/docs/12.x/database)
- [Query Builder](https://laravel.com/docs/12.x/queries)
- [Pagination](https://laravel.com/docs/12.x/pagination)
- [Migrations](https://laravel.com/docs/12.x/migrations)
- [Seeding](https://laravel.com/docs/12.x/seeding)
- [Redis](https://laravel.com/docs/12.x/redis)
- [MongoDB](https://laravel.com/docs/12.x/mongodb)
- ## Eloquent ORM
- [Getting Started](https://laravel.com/docs/12.x/eloquent)
- [Relationships](https://laravel.com/docs/12.x/eloquent-relationships)
- [Collections](https://laravel.com/docs/12.x/eloquent-collections)
- [Mutators / Casts](https://laravel.com/docs/12.x/eloquent-mutators)
- [API Resources](https://laravel.com/docs/12.x/eloquent-resources)
- [Serialization](https://laravel.com/docs/12.x/eloquent-serialization)
- [Factories](https://laravel.com/docs/12.x/eloquent-factories)
- ## AI
- [AI SDK](https://laravel.com/docs/12.x/ai-sdk)
- [MCP](https://laravel.com/docs/12.x/mcp)
- [Boost](https://laravel.com/docs/12.x/boost)
- ## Testing
- [Getting Started](https://laravel.com/docs/12.x/testing)
- [HTTP Tests](https://laravel.com/docs/12.x/http-tests)
- [Console Tests](https://laravel.com/docs/12.x/console-tests)
- [Browser Tests](https://laravel.com/docs/12.x/dusk)
- [Database](https://laravel.com/docs/12.x/database-testing)
- [Mocking](https://laravel.com/docs/12.x/mocking)
- ## Packages
- [Cashier (Stripe)](https://laravel.com/docs/12.x/billing)
- [Cashier (Paddle)](https://laravel.com/docs/12.x/cashier-paddle)
- [Dusk](https://laravel.com/docs/12.x/dusk)
- [Envoy](https://laravel.com/docs/12.x/envoy)
- [Fortify](https://laravel.com/docs/12.x/fortify)
- [Folio](https://laravel.com/docs/12.x/folio)
- [Homestead](https://laravel.com/docs/12.x/homestead)
- [Horizon](https://laravel.com/docs/12.x/horizon)
- [Mix](https://laravel.com/docs/12.x/mix)
- [Octane](https://laravel.com/docs/12.x/octane)
- [Passport](https://laravel.com/docs/12.x/passport)
- [Pennant](https://laravel.com/docs/12.x/pennant)
- [Pint](https://laravel.com/docs/12.x/pint)
- [Precognition](https://laravel.com/docs/12.x/precognition)
- [Prompts](https://laravel.com/docs/12.x/prompts)
- [Pulse](https://laravel.com/docs/12.x/pulse)
- [Reverb](https://laravel.com/docs/12.x/reverb)
- [Sail](https://laravel.com/docs/12.x/sail)
- [Sanctum](https://laravel.com/docs/12.x/sanctum)
- [Scout](https://laravel.com/docs/12.x/scout)
- [Socialite](https://laravel.com/docs/12.x/socialite)
- [Telescope](https://laravel.com/docs/12.x/telescope)
- [Valet](https://laravel.com/docs/12.x/valet)
- ## [API Documentation](https://api.laravel.com/docs/12.x/index.html)
- ## [Changelog](https://laravel.com/docs/changelog)
> **WARNING** You're browsing the documentation for an old version of Laravel. Consider upgrading your project to [Laravel 13.x](https://laravel.com/docs/13.x/providers).
# Service Providers
- [Introduction](https://laravel.com/docs/12.x/providers#introduction)
- [Writing Service Providers](https://laravel.com/docs/12.x/providers#writing-service-providers)
- [The Register Method](https://laravel.com/docs/12.x/providers#the-register-method)
- [The Boot Method](https://laravel.com/docs/12.x/providers#the-boot-method)
- [Registering Providers](https://laravel.com/docs/12.x/providers#registering-providers)
- [Deferred Providers](https://laravel.com/docs/12.x/providers#deferred-providers)
## [Introduction](https://laravel.com/docs/12.x/providers#introduction)
Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services, are bootstrapped via service providers.
But, what do we mean by "bootstrapped"? In general, we mean **registering** things, including registering service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application.
Laravel uses dozens of service providers internally to bootstrap its core services, such as the mailer, queue, cache, and others. Many of these providers are "deferred" providers, meaning they will not be loaded on every request, but only when the services they provide are actually needed.
All user-defined service providers are registered in the `bootstrap/providers.php` file. In the following documentation, you will learn how to write your own service providers and register them with your Laravel application.
If you would like to learn more about how Laravel handles requests and works internally, check out our documentation on the Laravel [request lifecycle](https://laravel.com/docs/12.x/lifecycle).
## [Writing Service Providers](https://laravel.com/docs/12.x/providers#writing-service-providers)
All service providers extend the `Illuminate\Support\ServiceProvider` class. Most service providers contain a `register` and a `boot` method. Within the `register` method, you should **only bind things into the [service container](https://laravel.com/docs/12.x/container)**. You should never attempt to register any event listeners, routes, or any other piece of functionality within the `register` method.
The Artisan CLI can generate a new provider via the `make:provider` command. Laravel will automatically register your new provider in your application's `bootstrap/providers.php` file:
```
1php artisan make:provider RiakServiceProviderphp artisan make:provider RiakServiceProvider
```
### [The Register Method](https://laravel.com/docs/12.x/providers#the-register-method)
As mentioned previously, within the `register` method, you should only bind things into the [service container](https://laravel.com/docs/12.x/container). You should never attempt to register any event listeners, routes, or any other piece of functionality within the `register` method. Otherwise, you may accidentally use a service that is provided by a service provider which has not loaded yet.
Let's take a look at a basic service provider. Within any of your service provider methods, you always have access to the `$app` property which provides access to the service container:
```
1<?php23namespace App\Providers;45use App\Services\Riak\Connection;6use Illuminate\Contracts\Foundation\Application;7use Illuminate\Support\ServiceProvider;89class RiakServiceProvider extends ServiceProvider10{11 /**12 * Register any application services.13 */14 public function register(): void15 {16 $this->app->singleton(Connection::class, function (Application $app) {17 return new Connection(config('riak'));18 });19 }20}<?php namespace App\Providers; use App\Services\Riak\Connection; use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\ServiceProvider; class RiakServiceProvider extends ServiceProvider { /** * Register any application services. */ public function register(): void { $this->app->singleton(Connection::class, function (Application $app) { return new Connection(config('riak')); }); } }
```
This service provider only defines a `register` method, and uses that method to define an implementation of `App\Services\Riak\Connection` in the service container. If you're not yet familiar with Laravel's service container, check out [its documentation](https://laravel.com/docs/12.x/container).
#### [The `bindings` and `singletons` Properties](https://laravel.com/docs/12.x/providers#the-bindings-and-singletons-properties)
If your service provider registers many simple bindings, you may wish to use the `bindings` and `singletons` properties instead of manually registering each container binding. When the service provider is loaded by the framework, it will automatically check for these properties and register their bindings:
```
1<?php23namespace App\Providers;45use App\Contracts\DowntimeNotifier;6use App\Contracts\ServerProvider;7use App\Services\DigitalOceanServerProvider;8use App\Services\PingdomDowntimeNotifier;9use App\Services\ServerToolsProvider;10use Illuminate\Support\ServiceProvider;1112class AppServiceProvider extends ServiceProvider13{14 /**15 * All of the container bindings that should be registered.16 *17 * @var array18 */19 public $bindings = [20 ServerProvider::class => DigitalOceanServerProvider::class,21 ];2223 /**24 * All of the container singletons that should be registered.25 *26 * @var array27 */28 public $singletons = [29 DowntimeNotifier::class => PingdomDowntimeNotifier::class,30 ServerProvider::class => ServerToolsProvider::class,31 ];32}<?php namespace App\Providers; use App\Contracts\DowntimeNotifier; use App\Contracts\ServerProvider; use App\Services\DigitalOceanServerProvider; use App\Services\PingdomDowntimeNotifier; use App\Services\ServerToolsProvider; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * All of the container bindings that should be registered. * * @var array */ public $bindings = [ ServerProvider::class => DigitalOceanServerProvider::class, ]; /** * All of the container singletons that should be registered. * * @var array */ public $singletons = [ DowntimeNotifier::class => PingdomDowntimeNotifier::class, ServerProvider::class => ServerToolsProvider::class, ]; }
```
### [The Boot Method](https://laravel.com/docs/12.x/providers#the-boot-method)
So, what if we need to register a [view composer](https://laravel.com/docs/12.x/views#view-composers) within our service provider? This should be done within the `boot` method. **This method is called after all other service providers have been registered**, meaning you have access to all other services that have been registered by the framework:
```
1<?php23namespace App\Providers;45use Illuminate\Support\Facades\View;6use Illuminate\Support\ServiceProvider;78class ComposerServiceProvider extends ServiceProvider9{10 /**11 * Bootstrap any application services.12 */13 public function boot(): void14 {15 View::composer('view', function () {16 // ...17 });18 }19}<?php namespace App\Providers; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; class ComposerServiceProvider extends ServiceProvider { /** * Bootstrap any application services. */ public function boot(): void { View::composer('view', function () { // ... }); } }
```
#### [Boot Method Dependency Injection](https://laravel.com/docs/12.x/providers#boot-method-dependency-injection)
You may type-hint dependencies for your service provider's `boot` method. The [service container](https://laravel.com/docs/12.x/container) will automatically inject any dependencies you need:
```
1use Illuminate\Contracts\Routing\ResponseFactory;23/**4 * Bootstrap any application services.5 */6public function boot(ResponseFactory $response): void7{8 $response->macro('serialized', function (mixed $value) {9 // ...10 });11}use Illuminate\Contracts\Routing\ResponseFactory; /** * Bootstrap any application services. */ public function boot(ResponseFactory $response): void { $response->macro('serialized', function (mixed $value) { // ... }); }
```
## [Registering Providers](https://laravel.com/docs/12.x/providers#registering-providers)
All service providers are registered in the `bootstrap/providers.php` configuration file. This file returns an array that contains the class names of your application's service providers:
```
1<?php23return [4 App\Providers\AppServiceProvider::class,5];<?php return [ App\Providers\AppServiceProvider::class, ];
```
When you invoke the `make:provider` Artisan command, Laravel will automatically add the generated provider to the `bootstrap/providers.php` file. However, if you have manually created the provider class, you should manually add the provider class to the array:
```
1<?php23return [4 App\Providers\AppServiceProvider::class,5 App\Providers\ComposerServiceProvider::class,6];<?php return [ App\Providers\AppServiceProvider::class, App\Providers\ComposerServiceProvider::class, ];
```
## [Deferred Providers](https://laravel.com/docs/12.x/providers#deferred-providers)
If your provider is **only** registering bindings in the [service container](https://laravel.com/docs/12.x/container), you may choose to defer its registration until one of the registered bindings is actually needed. Deferring the loading of such a provider will improve the performance of your application, since it is not loaded from the filesystem on every request.
Laravel compiles and stores a list of all of the services supplied by deferred service providers, along with the name of its service provider class. Then, only when you attempt to resolve one of these services does Laravel load the service provider.
To defer the loading of a provider, implement the `\Illuminate\Contracts\Support\DeferrableProvider` interface and define a `provides` method. The `provides` method should return the service container bindings registered by the provider:
```
1<?php23namespace App\Providers;45use App\Services\Riak\Connection;6use Illuminate\Contracts\Foundation\Application;7use Illuminate\Contracts\Support\DeferrableProvider;8use Illuminate\Support\ServiceProvider;910class RiakServiceProvider extends ServiceProvider implements DeferrableProvider11{12 /**13 * Register any application services.14 */15 public function register(): void16 {17 $this->app->singleton(Connection::class, function (Application $app) {18 return new Connection($app['config']['riak']);19 });20 }2122 /**23 * Get the services provided by the provider.24 *25 * @return array<int, string>26 */27 public function provides(): array28 {29 return [Connection::class];30 }31}<?php namespace App\Providers; use App\Services\Riak\Connection; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; class RiakServiceProvider extends ServiceProvider implements DeferrableProvider { /** * Register any application services. */ public function register(): void { $this->app->singleton(Connection::class, function (Application $app) { return new Connection($app['config']['riak']); }); } /** * Get the services provided by the provider. * * @return array<int, string> */ public function provides(): array { return [Connection::class]; } }
```
Copy as markdown
### On this page
- [Introduction](https://laravel.com/docs/12.x/providers#introduction)
- [Writing Service Providers](https://laravel.com/docs/12.x/providers#writing-service-providers)
- [The Register Method](https://laravel.com/docs/12.x/providers#the-register-method)
- [The Boot Method](https://laravel.com/docs/12.x/providers#the-boot-method)
- [Registering Providers](https://laravel.com/docs/12.x/providers#registering-providers)
- [Deferred Providers](https://laravel.com/docs/12.x/providers#deferred-providers)
[](https://laracon.us/)
Laravel is the most productive way to
build, deploy, and monitor software.
- Ā© 2026 Laravel
- [Legal](https://laravel.com/legal)
- [Status](https://status.laravel.com/)
#### Products
- [Cloud](https://cloud.laravel.com/)
- [Forge](https://forge.laravel.com/)
- [Nightwatch](https://nightwatch.laravel.com/)
- [Vapor](https://vapor.laravel.com/)
- [Nova](https://nova.laravel.com/)
#### Packages
- [Cashier](https://laravel.com/docs/billing)
- [Dusk](https://laravel.com/docs/dusk)
- [Horizon](https://laravel.com/docs/horizon)
- [Octane](https://laravel.com/docs/octane)
- [Scout](https://laravel.com/docs/scout)
- [Pennant](https://laravel.com/docs/pennant)
- [Pint](https://laravel.com/docs/pint)
- [Sail](https://laravel.com/docs/sail)
- [Sanctum](https://laravel.com/docs/sanctum)
- [Socialite](https://laravel.com/docs/socialite)
- [Telescope](https://laravel.com/docs/telescope)
- [Pulse](https://laravel.com/docs/pulse)
- [Reverb](https://laravel.com/docs/reverb)
- [Echo](https://laravel.com/docs/broadcasting)
#### Resources
- [Documentation](https://laravel.com/docs)
- [Starter Kits](https://laravel.com/starter-kits)
- [Release Notes](https://laravel.com/docs/releases)
- [Blog](https://laravel.com/blog)
- [News](https://laravel-news.com/)
- [Community](https://laravel.com/community)
- [Larabelles](https://larabelles.com/)
- [Learn](https://laravel.com/learn)
- [Jobs](https://larajobs.com/?partner=5)
- [Careers](https://laravel.com/careers)
- [Trust](https://trust.laravel.com/)
#### Partners
- [Tighten](https://laravel.com/partners/tighten)
- [Curotec](https://laravel.com/partners/curotec)
- [UCodeSoft](https://laravel.com/partners/ucodesoft)
- [byte5](https://laravel.com/partners/byte5)
- [64 Robots](https://laravel.com/partners/64-robots)
- [Jump24](https://laravel.com/partners/jump24)
- [Redberry](https://laravel.com/partners/redberry)
- [Kirschbaum](https://laravel.com/partners/kirschbaum)
- [CACI Limited](https://laravel.com/partners/caci-limited)
- [Steadfast Collective](https://laravel.com/partners/steadfast-collective)
- [See All](https://laravel.com/partners) |
| Readable Markdown | > **WARNING** You're browsing the documentation for an old version of Laravel. Consider upgrading your project to [Laravel 13.x](https://laravel.com/docs/13.x/providers).
- [Introduction](https://laravel.com/docs/12.x/providers#introduction)
- [Writing Service Providers](https://laravel.com/docs/12.x/providers#writing-service-providers)
- [The Register Method](https://laravel.com/docs/12.x/providers#the-register-method)
- [The Boot Method](https://laravel.com/docs/12.x/providers#the-boot-method)
- [Registering Providers](https://laravel.com/docs/12.x/providers#registering-providers)
- [Deferred Providers](https://laravel.com/docs/12.x/providers#deferred-providers)
## [Introduction](https://laravel.com/docs/12.x/providers#introduction)
Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services, are bootstrapped via service providers.
But, what do we mean by "bootstrapped"? In general, we mean **registering** things, including registering service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application.
Laravel uses dozens of service providers internally to bootstrap its core services, such as the mailer, queue, cache, and others. Many of these providers are "deferred" providers, meaning they will not be loaded on every request, but only when the services they provide are actually needed.
All user-defined service providers are registered in the `bootstrap/providers.php` file. In the following documentation, you will learn how to write your own service providers and register them with your Laravel application.
If you would like to learn more about how Laravel handles requests and works internally, check out our documentation on the Laravel [request lifecycle](https://laravel.com/docs/12.x/lifecycle).
## [Writing Service Providers](https://laravel.com/docs/12.x/providers#writing-service-providers)
All service providers extend the `Illuminate\Support\ServiceProvider` class. Most service providers contain a `register` and a `boot` method. Within the `register` method, you should **only bind things into the [service container](https://laravel.com/docs/12.x/container)**. You should never attempt to register any event listeners, routes, or any other piece of functionality within the `register` method.
The Artisan CLI can generate a new provider via the `make:provider` command. Laravel will automatically register your new provider in your application's `bootstrap/providers.php` file:
```
1php artisan make:provider RiakServiceProvider
```
### [The Register Method](https://laravel.com/docs/12.x/providers#the-register-method)
As mentioned previously, within the `register` method, you should only bind things into the [service container](https://laravel.com/docs/12.x/container). You should never attempt to register any event listeners, routes, or any other piece of functionality within the `register` method. Otherwise, you may accidentally use a service that is provided by a service provider which has not loaded yet.
Let's take a look at a basic service provider. Within any of your service provider methods, you always have access to the `$app` property which provides access to the service container:
```
1<?php23namespace App\Providers;45use App\Services\Riak\Connection;6use Illuminate\Contracts\Foundation\Application;7use Illuminate\Support\ServiceProvider;89class RiakServiceProvider extends ServiceProvider10{11 /**12 * Register any application services.13 */14 public function register(): void15 {16 $this->app->singleton(Connection::class, function (Application $app) {17 return new Connection(config('riak'));18 });19 }20}
```
This service provider only defines a `register` method, and uses that method to define an implementation of `App\Services\Riak\Connection` in the service container. If you're not yet familiar with Laravel's service container, check out [its documentation](https://laravel.com/docs/12.x/container).
#### [The `bindings` and `singletons` Properties](https://laravel.com/docs/12.x/providers#the-bindings-and-singletons-properties)
If your service provider registers many simple bindings, you may wish to use the `bindings` and `singletons` properties instead of manually registering each container binding. When the service provider is loaded by the framework, it will automatically check for these properties and register their bindings:
```
1<?php23namespace App\Providers;45use App\Contracts\DowntimeNotifier;6use App\Contracts\ServerProvider;7use App\Services\DigitalOceanServerProvider;8use App\Services\PingdomDowntimeNotifier;9use App\Services\ServerToolsProvider;10use Illuminate\Support\ServiceProvider;1112class AppServiceProvider extends ServiceProvider13{14 /**15 * All of the container bindings that should be registered.16 *17 * @var array18 */19 public $bindings = [20 ServerProvider::class => DigitalOceanServerProvider::class,21 ];2223 /**24 * All of the container singletons that should be registered.25 *26 * @var array27 */28 public $singletons = [29 DowntimeNotifier::class => PingdomDowntimeNotifier::class,30 ServerProvider::class => ServerToolsProvider::class,31 ];32}
```
### [The Boot Method](https://laravel.com/docs/12.x/providers#the-boot-method)
So, what if we need to register a [view composer](https://laravel.com/docs/12.x/views#view-composers) within our service provider? This should be done within the `boot` method. **This method is called after all other service providers have been registered**, meaning you have access to all other services that have been registered by the framework:
```
1<?php23namespace App\Providers;45use Illuminate\Support\Facades\View;6use Illuminate\Support\ServiceProvider;78class ComposerServiceProvider extends ServiceProvider9{10 /**11 * Bootstrap any application services.12 */13 public function boot(): void14 {15 View::composer('view', function () {16 // ...17 });18 }19}
```
#### [Boot Method Dependency Injection](https://laravel.com/docs/12.x/providers#boot-method-dependency-injection)
You may type-hint dependencies for your service provider's `boot` method. The [service container](https://laravel.com/docs/12.x/container) will automatically inject any dependencies you need:
```
1use Illuminate\Contracts\Routing\ResponseFactory;23/**4 * Bootstrap any application services.5 */6public function boot(ResponseFactory $response): void7{8 $response->macro('serialized', function (mixed $value) {9 // ...10 });11}
```
## [Registering Providers](https://laravel.com/docs/12.x/providers#registering-providers)
All service providers are registered in the `bootstrap/providers.php` configuration file. This file returns an array that contains the class names of your application's service providers:
```
1<?php23return [4 App\Providers\AppServiceProvider::class,5];
```
When you invoke the `make:provider` Artisan command, Laravel will automatically add the generated provider to the `bootstrap/providers.php` file. However, if you have manually created the provider class, you should manually add the provider class to the array:
```
1<?php23return [4 App\Providers\AppServiceProvider::class,5 App\Providers\ComposerServiceProvider::class,6];
```
## [Deferred Providers](https://laravel.com/docs/12.x/providers#deferred-providers)
If your provider is **only** registering bindings in the [service container](https://laravel.com/docs/12.x/container), you may choose to defer its registration until one of the registered bindings is actually needed. Deferring the loading of such a provider will improve the performance of your application, since it is not loaded from the filesystem on every request.
Laravel compiles and stores a list of all of the services supplied by deferred service providers, along with the name of its service provider class. Then, only when you attempt to resolve one of these services does Laravel load the service provider.
To defer the loading of a provider, implement the `\Illuminate\Contracts\Support\DeferrableProvider` interface and define a `provides` method. The `provides` method should return the service container bindings registered by the provider:
```
1<?php23namespace App\Providers;45use App\Services\Riak\Connection;6use Illuminate\Contracts\Foundation\Application;7use Illuminate\Contracts\Support\DeferrableProvider;8use Illuminate\Support\ServiceProvider;910class RiakServiceProvider extends ServiceProvider implements DeferrableProvider11{12 /**13 * Register any application services.14 */15 public function register(): void16 {17 $this->app->singleton(Connection::class, function (Application $app) {18 return new Connection($app['config']['riak']);19 });20 }2122 /**23 * Get the services provided by the provider.24 *25 * @return array<int, string>26 */27 public function provides(): array28 {29 return [Connection::class];30 }31}
``` |
| Shard | 63 (laksa) |
| Root Hash | 5292393658133462863 |
| Unparsed URL | com,laravel!/docs/12.x/providers s443 |