Service Providers
Service Providers are the central place of all application bootstrapping. Your own application, as well as all core services, are bootstrapped via service providers.
ServiceProvider Class
The abstract Ecommerce\Core\ServiceProvider class is the base class for all providers.
Register Services
Register bindings in the service container.
Description:
The register() method is where you 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 might accidentally use a service that is provided by a service provider which has not loaded yet.
Method Signature:
abstract public function register(): void
Examples:
namespace Ecommerce\Providers;
use Ecommerce\Core\ServiceProvider;
use Ecommerce\Services\PaymentGateway;
class PaymentServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(PaymentGateway::class, function ($app) {
return new PaymentGateway();
});
}
}
Boot Services
Bootstrap any application services.
Description:
The boot() method is called after all other service providers have been registered. This makes it the perfect place to register event listeners, routes, or any other functionality that relies on other services.
Method Signature:
public function boot(): void
Examples:
namespace Ecommerce\Providers;
use Ecommerce\Core\ServiceProvider;
use Ecommerce\Core\Discovery\PolicyDiscovery;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// Discover policies
$this->app->make(PolicyDiscovery::class)->discover();
// Register scheduler
Scheduler::setup();
}
}
AppServiceProvider
The main service provider for your application.
Default Structure
The standard AppServiceProvider included with the application.
Description:
The Ecommerce\Providers\AppServiceProvider class is a great place to start adding your own application bootstrapping. It registers core managers, database services, and discoveries.
Examples:
// Creating singletons
protected function register_managers()
{
$this->app->singleton(LogManager::class, fn() => new LogManager());
$this->app->singleton(EventManager::class, fn() => new EventManager());
// ...
}
// Booting services
public function boot()
{
// ...
}