Skip to content
Antares

Service Providers

Service providers are the only place where you configure the container and register routes. Every provider implements the Antares\ServiceProvider interface:

interface ServiceProvider
{
public function register(Container $container): void;
}

Use providers to bind interfaces to concrete implementations and register singletons for classes that need primitive constructor arguments (like values from .env)

scoped() works like singleton(), but the instance is thrown away at the start of every request. Use it for classes that hold per-request state when running in worker mode (Swoole, FrankenPHP). Under PHP-FPM the distinction does not matter, since the process ends after every request. See Bindings & Singletons.

use Antares\ServiceProvider;
use Antares\Container\Container;
class AppServiceProvider implements ServiceProvider
{
public function register(Container $container): void
{
$container->bind(LoggerInterface::class, FileLogger::class);
$container->singleton(Mailer::class, fn() => new Mailer(
host: $_ENV['MAIL_HOST'],
port: (int) $_ENV['MAIL_PORT'],
secret: $_ENV['MAIL_SECRET'],
));
$container->singleton(Database::class, fn() => new Database(
dsn: "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']}",
username: $_ENV['DB_USERNAME'],
password: $_ENV['DB_PASSWORD'],
));
$container->scoped(RequestContext::class, fn() => new RequestContext());
}
}

Route providers call $router->register() with each of your controller classes. The router inspects the class via reflection and registers all methods annotated with route attributes:

use Antares\ServiceProvider;
use Antares\Container\Container;
use Antares\Router\Router;
class RouteServiceProvider implements ServiceProvider
{
public function register(Container $container): void
{
$router = $container->make(Router::class);
$router->register(UserController::class);
$router->register(PostController::class);
$router->register(AuthController::class);
}
}

Alternatively, define routes in config/routes.php and load them with registerFromConfig():

config/routes.php
return [
['GET', '/users', UserController::class, 'index', 200],
['POST', '/users', UserController::class, 'store', 201],
['GET', '/users/{id}', UserController::class, 'show', 200],
['PUT', '/users/{id}', UserController::class, 'update', 200],
['DELETE', '/users/{id}', UserController::class, 'destroy', 204],
];
$router->registerFromConfig(require __DIR__ . '/../config/routes.php');

Supported via symfony/yaml:

Terminal window
composer require symfony/yaml
config/routes.yaml
routes:
- method: GET
path: /users
controller: App\Controllers\UserController
action: index
status: 200
- method: POST
path: /users
controller: App\Controllers\UserController
action: store
status: 201
$router->registerFromYaml(__DIR__ . '/../config/routes.yaml');

Bridge packages declare their providers in composer.json and are registered automatically, so you don’t add them to ->providers([...]). Antares reads them from vendor/composer/installed.json on boot and registers them before your own providers, so a binding in your provider replaces the bridge’s:

"extra": {
"antares": {
"providers": [
"Antares\\Monolog\\MonologServiceProvider"
]
}
}