symfony/contracts
Symfony Contracts – Standardized PHP interfaces for loose coupling, ensuring interoperability with Symfony and third-party implementations. Useful for dependency injection, autowiring, and framework-agnostic design. Backward-compatible with Symfony components.
Install symfony/contracts via Composer: composer require symfony/contracts. This meta-package pulls in all domain-specific contracts (e.g., symfony/cache-contracts, symfony/event-dispatcher-contracts, symfony/translation-contracts). Start by identifying a concrete use case—like caching or event handling—and choose the appropriate interface (e.g., Symfony\Contracts\Cache\CacheInterface). Then install a compatible implementation: typically a Symfony component (symfony/cache, symfony/event-dispatcher) or a third-party library that declares the contract in its provide section. In Laravel, your first real win comes from replacing framework-specific type hints (e.g., CacheManager) with the Symfony contract in non-Laravel-bound services or packages.
Symfony\Contracts\Cache\CacheInterface or Symfony\Contracts\EventDispatcher\EventDispatcherInterface instead of Laravel’s concrete facades or repositories. This decouples business logic from the underlying framework.AppServiceProvider, use alias() to map Symfony contracts to Laravel equivalents:
$this->app->alias(\Symfony\Contracts\Cache\CacheInterface::class, \Illuminate\Cache\Repository::class);
$this->app->alias(\Symfony\Contracts\EventDispatcher\EventDispatcherInterface::class, \Illuminate\Events\Dispatcher::class);
This enables autowiring—your classes can type-hint Symfony interfaces and still use Laravel’s underlying implementations.symfony/contracts. Consumers of your package can then plug in their own implementations (Symfony, Laravel, Doctrine, etc.) without your code knowing the difference.symfony/mailer underpins Mail::to()->send()). When building your own mailer or queueing abstractions, implement Symfony\Contracts\Translation\TranslatorInterface or Symfony\Contracts\Translation\TranslatorAwareInterface for seamless integration.symfony/contracts is only interfaces and traits. No configuration, no commands, no database migrations. All behavior lives in the implementation packages.symfony/contracts is a meta-package, you usually only need specific contract packages (e.g., symfony/translation-contracts). Install only what you use (composer require symfony/cache-contracts) to avoid bloat.CacheInterface extends Psr\SimpleCache\CacheInterface) but add richer functionality (e.g., retry strategies, tagged invalidation). Always inspect docblocks—don’t assume 1:1 parity.Target class does not exist errors for interfaces, verify your alias() calls or register explicit bindings.composer why symfony/cache-contracts and composer why-not symfony/cache-contracts to trace dependency conflicts. Also, inspect installed packages with composer show -i | grep symfony/*-contracts to confirm which contract packages you’re actually using.How can I help you explore Laravel packages today?