Docs / Architecture Concepts / Service Providers
Service Providers
Introduction
Service providers are the central place of all Laraflare application bootstrapping. Your own application, as well as all of Laraflare'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.
Laraflare 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.ts file. In the following documentation, you will learn how to write your own service providers and register them with your Laraflare application.
Note
If you would like to learn more about how Laraflare handles requests and works internally, check out our documentation on the Laraflare request lifecycle.
Writing Service Providers
All service providers extend the 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. Laraflare will automatically register your new provider in your application's bootstrap/providers.ts file:
npx 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:
import { config, ServiceProvider } from '@laraflare/framework';
import { Connection } from '../Services/Riak/Connection';
export class RiakServiceProvider extends ServiceProvider {
/**
* Register any application services.
*/
override register(): void {
this.app.singleton(Connection, () => new Connection(config('riak')));
}
}
This service provider only defines a register method, and uses that method to define an implementation of Connection in the service container. If you're not yet familiar with Laraflare'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:
import { ServiceProvider } from '@laraflare/framework';
import { DowntimeNotifier } from '../Contracts/DowntimeNotifier';
import { ServerProvider } from '../Contracts/ServerProvider';
import { DigitalOceanServerProvider } from '../Services/DigitalOceanServerProvider';
import { PingdomDowntimeNotifier } from '../Services/PingdomDowntimeNotifier';
import { ServerToolsProvider } from '../Services/ServerToolsProvider';
export class AppServiceProvider extends ServiceProvider {
/**
* All of the container bindings that should be registered.
*/
override bindings = [
[ServerProvider, DigitalOceanServerProvider],
] as Array<[typeof ServerProvider, typeof DigitalOceanServerProvider]>;
/**
* All of the container singletons that should be registered.
*/
override singletons = {
[DowntimeNotifier.name]: PingdomDowntimeNotifier,
[ServerProvider.name]: ServerToolsProvider,
};
}
Both properties take either a list of [abstract, concrete] pairs or an object of { [abstract]: concrete }.
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:
import { ServiceProvider } from '@laraflare/framework';
export class ComposerServiceProvider extends ServiceProvider {
/**
* Bootstrap any application services.
*/
override boot(): void {
// ...
}
}
Note
Laraflare's screens are Vue (
resources/js), so there is no Blade view composer to register; pass what a screen needs as its props from the route or controller.
A provider may also run a callback before or after its own boot method with this.booting(callback) and this.booted(callback).
Boot Method Dependency Injection
Laraflare does not inject arguments into a provider's boot method: it is called with none. Resolve what you need from the service container with this.app.make():
override boot(): void {
const events = this.app.make('events');
// ...
}
Registering Providers
All service providers are registered in the bootstrap/providers.ts configuration file. This file exports an array that contains the classes of your application's service providers:
import { AppServiceProvider } from '../app/Providers/AppServiceProvider';
export const providers = [AppServiceProvider];
When you invoke the make:provider Artisan command, Laraflare will automatically add the generated provider to the bootstrap/providers.ts file. However, if you have manually created the provider class, you should manually import it and add it to the array:
import { AppServiceProvider } from '../app/Providers/AppServiceProvider';
import { ComposerServiceProvider } from '../app/Providers/ComposerServiceProvider';
export const providers = [AppServiceProvider, ComposerServiceProvider];
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 registered on every request.
Laraflare 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 Laraflare load the service provider.
To defer the loading of a provider, define a provides method and have isDeferred return true. The provides method should return the service container bindings registered by the provider:
import { ServiceProvider } from '@laraflare/framework';
import { Connection } from '../Services/Riak/Connection';
export class RiakServiceProvider extends ServiceProvider {
/**
* Register any application services.
*/
override register(): void {
this.app.singleton(Connection, (app) => new Connection(app.make('config').get('riak')));
}
/**
* Get the services provided by the provider.
*/
override provides(): Array<typeof Connection> {
return [Connection];
}
/**
* Determine if the provider is deferred.
*/
override isDeferred(): boolean {
return true;
}
}