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

Di Laravel Package

nette/di

Nette DI is a fast, configurable dependency injection container for PHP. Compile-time container generation boosts performance, while extensions, autowiring, and service definitions make complex apps easy to wire. Integrates smoothly with the Nette framework or standalone.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing via Composer: composer require nette/di. Define services in a NEON config file (e.g., services.neon) using simple key-value syntax—Nette auto-detects services by type-hints and uses autowiring by default. For a basic setup:

services:
    - App\Model\UserRepository
    App\Service\EmailService: [@App\Model\UserRepository]
parameters:
    debugMode: %env.ADAPTIVE_DEBUG%

Then compile the container:

$compiler = new Nette\DI\Compiler();
$compiler->addConfig(['services' => [...]]); // or load from NEON
$container = $compiler->compile();

First use case: Inject services via constructor autowiring in your classes—no configuration needed for most cases. Leverage richer PHPDoc and native types for better IDE support (e.g., @param App\Model\UserRepository $userRepo).


Implementation Patterns

  • Autowiring-first design: Rely on type-hinted constructor dependencies—Nette resolves them automatically using PHP reflection. New: PHPDoc generics (e.g., @param array<App\Model\User> $users) improve autowiring for collections.
  • Explicit service definitions for non-trivial cases (e.g., custom factories, non-public constructors):
    services:
        cacheFactory:
            factory: App\Factory\CacheFactory::create()
            setup:
                - setTtl(3600)
    
  • Dynamic parameters: Use %env.VAR% or parameters section—expanded at compile time. Tip: Native types in ContainerBuilder ensure stricter validation for runtime parameters.
  • Lazy services: Declare lazy: true to defer instantiation:
    services:
        dbConnection:
            lazy: true
            factory: PDO
    
  • Extension customization: Extend Nette\DI\CompilerExtension—now with PHPStan-clean internals, reducing edge-case bugs.
  • PHP 8 attributes: Use #[Nette\DI\Attributes\Inject] for injection where constructors are impractical (e.g., controllers). New: IDEs now better infer types from PHPDoc in attributes.

Gotchas and Tips

  • BC breaks: None in v3.2.6, but static analysis is now mandatory—ensure your CI runs PHPStan with nette/di rules.
  • Dynamic parameters misuse: %parameters% expands only static params—avoid for runtime values. Use getParameter() at runtime instead.
  • Nullable/fallback type handling: Explicit nullable types (?Type) or union types improve reliability. New: Native types in Resolver now enforce stricter checks.
  • Circular references: Use @self in setup or lazy loading to break cycles.
  • Compilation caching: Always set $tempDir—reduces cold-start overhead. New: Tracy’s ContainerPanel now uses n:attributes for cleaner debugging.
  • Container debugging: Use $container->getService('debug Tracy') or dump()new Latte rewrite makes the panel more intuitive.
  • Generics in autowiring: Prefer @param array<ClassName> in PHPDoc for collections (e.g., array<App\Model\User>).
  • Named parameters only: Factories must use named args—positional args are forbidden since v3.2.4.
  • Performance tip: Mark rarely-used services as lazy and group extensions in separate config files to reduce compile time.
  • IDE support: New PHPDoc across Compiler, Container, and Resolver improves autocompletion and type hints. Example:
    /** @param array<App\Service\LoggerInterface> $loggers */
    public function __construct(array $loggers) {}
    
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.
hamzi/corewatch
minionfactory/raw-hydrator
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