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

Dependency Injection Laravel Package

symfony/dependency-injection

Symfony DependencyInjection standardizes and centralizes object construction with a powerful service container. Define, configure, and wire services, manage parameters and service lifecycles, and enable autowiring and compilation for efficient, testable apps.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup in Laravel

  1. Installation

    composer require symfony/dependency-injection
    

    Laravel already uses this under the hood (via Illuminate/Container), but standalone usage is possible.

  2. First Use Case: Manual Container Initialization

    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\DependencyInjection\Reference;
    
    $container = new ContainerBuilder();
    $container->register('app.service', MyService::class)
        ->setArguments([new Reference('app.other_service')]);
    
    $container->register('app.other_service', OtherService::class);
    
    $service = $container->get('app.service');
    
  3. Where to Look First

    • Symfony DI Component Docs
    • Laravel’s Illuminate\Container\Container (for integration patterns)
    • ContainerBuilder and Definition classes for core functionality.

Implementation Patterns

1. Service Registration & Configuration

  • Dynamic Registration
    $container->autowire(MyService::class)
        ->setPublic(true) // Expose as a service
        ->setAutowired(true); // Enable autowiring
    
  • Tagging Services (for compiler passes or lazy loading)
    $definition = $container->register(MyListener::class);
    $definition->addTag('kernel.event_listener', ['event' => 'app.event']);
    

2. Integration with Laravel

  • Extend Laravel’s Container
    $container = app();
    $container->extend('app.service', function ($app, $service) {
        $service->setConfig($app['config']['services']);
        return $service;
    });
    
  • Replace Laravel’s Container (Advanced)
    $container = new ContainerBuilder();
    // Register Laravel bindings manually...
    $app = new LaravelApp($container);
    

3. Compiler Passes (Custom Logic)

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class MyCompilerPass implements CompilerPassInterface {
    public function process(ContainerBuilder $container) {
        if (!$container->has('app.service')) return;
        $definition = $container->findDefinition('app.service');
        $definition->addMethodCall('setPostConfig', [['value' => 42]]);
    }
}

Register via:

$container->addCompilerPass(new MyCompilerPass());

4. Parameter Bag for Config

$container->setParameter('app.debug', env('APP_DEBUG', false));
$container->setParameter('app.services', [
    'timeout' => 30,
]);

Access in services:

$debug = $container->getParameter('app.debug');

Gotchas and Tips

Pitfalls

  1. Circular Dependencies

    • Symfony DI throws CircularReferenceException. Use setLazy(true) or refactor.
    $definition->setLazy(true); // Lazy-load to break cycles
    
  2. Autowiring Conflicts

    • Laravel’s autowiring may override Symfony DI’s. Explicitly register services if needed:
    $container->autowire(MyService::class)->setPublic(false);
    
  3. Parameter Overrides

    • Parameters set via setParameter() replace existing ones. Use setParameterBag() for bulk updates.
  4. Service Not Found

    • Check for typos in service IDs or missing setPublic(true). Use:
    $container->has('service.id'); // Verify existence
    

Debugging Tips

  • Dump Container Contents
    dump($container->getServiceIds());
    dump($container->getDefinition('service.id'));
    
  • Enable Debug Mode
    $container->setDebug(true); // Shows detailed errors
    

Extension Points

  1. Custom Service Locators
    $container->set('app.locator', new class {
        public function find($id) { /* custom logic */ }
    });
    
  2. Override Service Definitions
    $container->setDefinition('app.service', new Definition(MyService::class));
    
  3. Use ContainerInterface for Read-Only Access
    $container->get('service.id'); // No modifications allowed
    

Laravel-Specific Quirks

  • Avoid Mixing app() and $container Laravel’s app() is a ContainerInterface wrapper. Prefer direct $container usage for DI features.
  • Priority in Service Resolution Laravel’s bindings take precedence. Use setPublic(false) to force Symfony DI resolution:
    $container->register('app.service', MyService::class)->setPublic(false);
    
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai