Service Providers
SigmaPHP uses a Service Container to manage application dependencies and enable Dependency Injection (DI). Instead of manually creating and passing objects throughout your application, the container acts as a central registry that knows how to build and provide the services your application requires.
A service can be any reusable object in your application such as database connections, loggers, mailers, configuration loaders, or any custom component you define. By registering these services in the container, SigmaPHP can resolve and inject them wherever they are needed.
What is the Container?
The Container is responsible for storing and resolving service definitions.
Whenever a class or component requires a dependency, the container will create or return
the appropriate instance.
This approach keeps your application loosely coupled and makes components easier to maintain, test, and extend.
How the Kernel Glues Everything Together
During the application bootstrap process, the Kernel initializes the framework and
loads all configured service providers. The kernel creates a container instance and then
executes each provider in two stages: register and boot.
This lifecycle ensures that all services are first defined and registered inside the container before any service attempts to interact with another dependency.
In short, the process looks like this:
Kernel starts
↓
Container is created
↓
All Service Providers → register()
↓
All Service Providers → boot()
↓
Application is ready
Once services are registered in the container, SigmaPHP can automatically inject them into controllers and
action
methods. This means you do not need to manually create objects or manage their dependencies—the container
will
resolve and provide them when needed. For example, if a controller requires a Logger service,
you can
simply type-hint it in the constructor and the container will automatically supply the correct instance when
the
controller is created.
<?php
namespace App\Controllers;
use App\Services\Logger;
class UserController
{
protected $logger;
public function __construct(Logger $logger)
{
$this->logger = $logger;
}
public function index()
{
$this->logger->info('Users page visited');
}
}
The container detects the Logger dependency, resolves it from the registered services,
and injects it automatically when the controller is instantiated.
Service Providers
Service Providers are the main place where services are registered inside the container. They allow you to organize your application's dependencies in a clear and structured way.
Each provider must implement the ServiceProviderInterface.
This interface requires two methods to be implemented.
Creating a Service Provider
SigmaPHP provides a CLI command that can generate a new service provider automatically.
./bin/sigma-cli create:service-provider AppServiceProvider
This command will create a new provider class inside your application's providers directory.
Using Service Providers
The ServiceProviderInterface requires two methods to be implemented in your service provider:
-
register(Container $container): void
The register method has access to the current instance of the container. It is used to define and register dependencies that do not perform any operations or depend on other services during initialization. -
boot(Container $container): void
The boot method is similar to the register method, but it is executed only after all services have been registered in the container. This makes it the ideal place to perform operations that rely on other services.
Example Service Provider
<?php
namespace App\Providers;
use SigmaPHP\Container\Interfaces\ServiceProviderInterface;
use SigmaPHP\Container\Container;
/**
* App Service Provider Class
*/
class AppServiceProvider implements ServiceProviderInterface
{
/**
* The boot method , will be called after all
* dependencies were defined in the container.
*
* @param Container $container
* @return void
*/
public function boot(Container $container)
{
shareTemplateVariable([
'githubLink' => 'https://github.com/SigmaPHP/SigmaPHP',
'docsLink' => 'https://www.sigmaphp.com/docs'
]);
}
/**
* Add a definition to the container.
*
* @param Container $container
* @return void
*/
public function register(Container $container)
{
//
}
}
In this example, the provider uses the boot() method to share global variables with
the template engine after the application has been fully initialized.
As your application grows, you may create multiple service providers to organize different parts of your system such as database services, authentication, caching, or third-party integrations.
Using service providers together with the container allows SigmaPHP to maintain a clean architecture where dependencies are defined in one place and resolved automatically throughout the application.