Core

Usage Patterns

Recommended dependency usage: constructor injection first, provider-based registration, aliases only in explicit container wiring.

Constructor injection first

Application code should primarily depend on class/interface IDs through constructor injection.

public function __construct(
    private readonly \Lemonade\Framework\Routing\UrlGenerator $url,
) {}

Provider factory access

Use explicit container access in provider factories to compose services from existing registrations.

$container->singleton(InvoiceImporter::class, static function (ContainerInterface $container): InvoiceImporter {
    return new InvoiceImporter(
        $container->get(ApiClient::class),
    );
});

Aliases in provider wiring

Use aliases only when a provider defines them and only where the container is explicitly available. They are not a replacement for constructor injection and they are not exposed through a global service helper.

$container->singleton(PageRenderer::class, static function (ContainerInterface $container): PageRenderer {
    return new PageRenderer(
        $container->get('url'),
        $container->get('validator'),
    );
});