Introduction
Windwalker DI is a dependency injection tools, it provides an IoC container to manage objects and dependencies.
We also support service provider to help developers build their service in a universal interface.
In 4.x, Windwalker DI has been totally rewrote.
Installation
Install via composer
composer require windwalker/di ^4.0
Use in Windwalker
Dependency Injection
DI Container is part of Windwalker, you don't need to use it directly. You can inject it in any service or custom class.
class MyClass
{
public function __construct(protected FooService $fooService)
{
}
}
Then, register it to etc/di.php
:
// ...
[
'bindings' => [
FooService::class,
MyClass::class
]
]
Now, you can inject your class in View or Controller:
#[Controller]
class DashboardController
{
public function index(MyClass $myClass)
{
//
}
}
Directly use Container
If you want to directly use Container, you can inject itself.
use \Windwalker\DI\Container;
#[Controller]
class DashboardController
{
public function index(Container $container)
{
$fooService = $container->get(FooService::class);
}
}
Or use AppContext
, it is a container wrapper:
use \Windwalker\Core\Application\AppContext;
#[Controller]
class DashboardController
{
public function index(AppContext $app)
{
$fooService = $app->make(FooService::class);
}
}
Use as Standalone Component
If you want to use Container without Windwalker core, just create it.
$container = new \Windwalker\DI\Container();
$container->share(MyClass::class, new MyClass());
$myClass = $container->get(MyClass::class);