Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Container Laravel Package

illuminate/container

Illuminate Container is Laravel’s lightweight dependency injection container for resolving classes, managing bindings, singletons, contextual dependencies, and automatic constructor injection. It powers service resolution and inversion of control in Laravel apps and packages.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer (composer require illuminate/container) or using it within a Laravel app (it’s bundled in laravel/framework). Begin with resolving a simple class:

$container = new Illuminate\Container\Container();
$container->bind(\Psr\Log\LoggerInterface::class, \App\Logging\ConsoleLogger::class);
$logger = $container->make(\Psr\Log\LoggerInterface::class);

Explore the API by checking bind(), singleton(), instance(), make(), and resolve() — these cover 90% of day-to-day usage. Use Container::getInstance() only when globally sharing a single container instance.

Implementation Patterns

  • Interface Binding + Resolving: Bind interfaces to concrete types to decouple logic and enable testing (e.g., bind(NotificationSenderInterface::class, SlackSender::class)). Resolve them anywhere via make() or constructor injection.
  • Contextual Binding: Use when(...)->needs(...)->give(...) to inject different implementations based on consumer class (e.g., production vs. staging storage for a StorageInterface in UploadController vs. ImportController).
  • Tagging for Plugins or Lists: Tag related bindings ($container->tag('formatter', [JsonFormatter::class, XmlFormatter::class])) and resolve all at once ($container->tagged('formatter')), ideal for extension points like event listeners or formatters.
  • Method Injection via call(): Resolve dependencies dynamically for callbacks (e.g., $container->call([$controller, 'index'])) — crucial for event dispatching, CLI commands, and custom routing.
  • Extending Bindings Post-Resolution: Use extend() to wrap resolved services (e.g., add logging, caching, or retries) without modifying the original class:
    $container->extend(ApiClient::class, function ($client, $container) {
        return new RetryDecorator($client);
    });
    

Gotchas and Tips

  • Avoid modifying the global instance: Container::setInstance() affects the entire process — dangerous in CLI workers (e.g., Horizon, queue listeners). Prefer local containers or explicit instantiation unless resetting context intentionally.
  • Circular dependency detection is runtime-only: If A injects B and B injects A, you’ll get a CircularDependencyException at resolve time — prevent by injecting interfaces, using lazy proxies, or deferring with make() in the constructor.
  • Parameterized resolution: Use makeWith() or contextual needs('$variable')->give($value) when constructor parameters aren’t type-hinted or vary at runtime (e.g., injecting a config key or dynamic ID).
  • PSR-11 compatibility ≠ feature parity: While it implements Psr\Container\ContainerInterface, Laravel-specific methods like tag(), extend(), build() won’t work outside Laravel — avoid relying on them for portability.
  • Debug resolution state: resolved($abstract) tells if a binding is already instantiated, and build() lets you inspect how a class would be resolved (useful for debugging missing bindings or double resolution).
  • Extensibility hooks: Register callbacks via resolver or override make() / build() for cross-cutting concerns (e.g., auto-instrumentation, lazy-loading proxies, or DI tracing).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests