Skip to content

Container

The container is responsible for managing dependencies and providing instances of services to various components of the application.

Binding Services

Services can be bound to the container using two methods: bind and bindScoped.

Using bind

php
$container->bind(new HelloService);

INFO

An instance of the HelloService class is bound to the container, allowing for more flexibility in managing service instances, especially when specific configurations or dependencies need to be set up before binding.

Using bindScoped

php
$container->bindScoped(HelloService::class);

INFO

The HelloService class is bound to the container using its class name as a string, providing a simpler and more straightforward approach when no additional setup is required before binding.

Instantiating Services

Services can be instantiated in various contexts within the application, such as:

In Controller Constructor

php
class MyController
{
    protected $helloService;

    public function __construct(HelloService $helloService)
    {
        $this->helloService = $helloService;
    }
}

INFO

The HelloService is injected into the constructor of the MyController class.

In Route Handler Method

php
class MyController
{
    public function handleRequest(ServerRequest $request, RequestArguments $args, HelloService $helloService)
    {
        // Handle request with injected services
    }
}

INFO

The HelloService is injected into a route handler method of the MyController class.

In Closure

php
$route->get('/hello', function(ServerRequest $request, RequestArguments $args, HelloService $helloService)
{
    // Handle request with injected services
});

INFO

The HelloService is injected into a closure defined within a route.

Released under the MIT License.