Guide
Services & Providers
Service registration, interface bindings, and explicit application composition.
Register services in AppServiceProvider
Application composition belongs in App\Providers\AppServiceProvider. Use singleton() for shared services and set() for services that should be created repeatedly.
<?php
declare(strict_types=1);
namespace App\Providers;
use App\Services\ArticleRepository;
use Lemonade\Framework\Container\ContainerInterface;
use Lemonade\Framework\Core\ServiceProviderInterface;
final class AppServiceProvider implements ServiceProviderInterface
{
public function register(ContainerInterface $container): void
{
$container->singleton(ArticleRepository::class, ArticleRepository::class);
}
}Pass dependencies through constructors
Controllers and services should declare their dependencies in the constructor. This keeps the dependency graph readable and makes the code easier to test.
<?php
declare(strict_types=1);
namespace App\Controllers;
use App\Services\ArticleRepository;
use Lemonade\Framework\Core\AbstractController;
use Psr\Http\Message\ResponseInterface;
final class ArticleController extends AbstractController
{
public function __construct(
private readonly ArticleRepository $articles,
) {}
public function detail(int $id): ResponseInterface
{
return $this->json($this->articles->find($id));
}
}Interfaces and typed configuration
Interfaces require an explicit binding. At runtime, consume YAML configuration through typed config objects rather than scattered getenv() calls.
<?php
declare(strict_types=1);
namespace App\Providers;
use App\Services\NewsletterSender;
use App\Services\NewsletterSenderInterface;
use Lemonade\Framework\Container\ContainerInterface;
use Lemonade\Framework\Core\ServiceProviderInterface;
final class AppServiceProvider implements ServiceProviderInterface
{
public function register(ContainerInterface $container): void
{
$container->singleton(NewsletterSenderInterface::class, NewsletterSender::class);
}
}