PHP Luminova: Routable Handler's Dependency Injection Resolver
Resolve type-hinted dependencies in HTTP and CLI controller methods and route closures. Register custom bindings and pass captured route values to the same handler.
Luminova's Dependency Injection Resolver system automatically creates and passes the objects your routable methods need. You don't have to manually set them up, which keeps your code cleaner and easier to manage.
Type-hint the dependency in a routable method or closure. Register a concrete class when the handler expects an interface or when you want to control the implementation.
What it does
- Creates and injects dependencies automatically — no manual wiring required.
- Supports class bindings for selecting an implementation.
- Works with controller methods and route closures right out of the box.
- Handles dependencies and URL parameters at the same time within a single method.
Enabling Routing System Dependency Injection
Check the setting in your project’s .env file. To enable routing dependency injection, use the following value.
feature.route.dependency.injection = enableNote: DI only works with routable methods — not with controller constructors.
Using Dependency Injection
1. Create an Injectable Class
First, let's make a simple class called Test that we'll inject later.
// /app/Utils/Test.php
namespace App\Utils;
class Test
{
public function __construct(private string $message = 'Hello world!') {}
public function getMessage(): string
{
return $this->message;
}
}2. Bind the Class to the DI Container
Next, we tell Luminova how to create this class. You can bind it from the application class helper method or using the static DependencyResolver::bind() method, depending on your preference.
// /app/Application.php
namespace App;
use App\Utils\Example;
use App\Utils\ExampleInterface;
use Luminova\Foundation\Core\AbstractApplication;
class Application extends AbstractApplication
{
protected function onPreCreate(): void
{
// Register Test as its own implementation
$this->bind(ExampleInterface::class, Example::class);
// Alternative way (also valid)
// \Luminova\Routing\DependencyResolver::bind(Test::class, Test::class);
}
}3. Inject into a Controller Method
Controller methods can accept both URI parameters and type-hinted dependencies in any order. The DI engine figures out what belongs where.
// /app/Controllers/Http/Controller.php
namespace App\Controllers\Http;
use App\Utils\TestInterface;
use Luminova\Attributes\Route;
use Luminova\Base\Http\AbstractController;
class MyController extends AbstractController
{
#[Route('/test/(:username)', methods: ['GET'])]
public function test(ExampleInterface $example, string $username): int
{
return $this->view('test', [
'username' => $username,
'message' => $example->getMessage(), /* Outputs: Hello world! */
]);
}
}$exampleis automatically resolved and injected by Luminova.$usernamecomes from the URI segment.
4. Inject into a Route Closure
DI also works inside closures. Just type-hint your dependencies, and Luminova provides them.
// /routes/web.php
use Luminova\Routing\Router;
use App\Utils\ExampleInterface;
use Luminova\Foundation\Core\AbstractApplication;
Router::get('/test/(:username)', static function(AbstractApplication $app, ExampleInterface $example, string $username): int {
return $app->view->view('test', [
'username' => $username,
'message' => $example->getMessage(), /* Outputs: Hello world! */
]);
});5. Works with CLI Command Groups Too
CLI command handlers can receive dependencies too. This route context registers a command in the http-get group.
// /routes/cli.php
use Luminova\Routing\Router;
use Luminova\Interface\RouterInterface;
Router::group('http-get', static function(RouterInterface $router): void {
Router::command('request', 'Command::test');
});Reminder:Ensure that injected class type-hinted are correct and can be resolved by Luminova (either automatically or via a bind).
Dependency Injections
Luminova's DI system allows you to map abstract classes or interfaces to real, concrete implementations. This makes it simple for the framework to resolve and inject dependencies automatically when routing requests.
You can configure bindings in your application using either:
- The static class method:
Luminova\Routing\DependencyResolver::bind() - The application helper method:
$this->bind()inside yourApplicationclass (onCreate()or__construct()).
Binding Dependencies
Bind an interface or class to an implementation that the container can instantiate without arguments.
Resolving an Interface to a Concrete Class
Bind an interface to the class that implements it:
use Luminova\Routing\DependencyResolver;
// Using the DI static bind method directly
DependencyResolver::bind(
ExampleInterface::class,
Example::class
);
// Using the Application helper method
// Inside \App\Application::onCreate() or \App\Application::__construct()
$this->bind(
ExampleInterface::class,
Example::class
);Usage inside a routable method:
public function myExample(ExampleInterface $example): int
{
// Handle example
}By type-hinting the interface, you can swap implementations later without changing your controller method.
Configure an Implementation Through Its Constructor
For a service that needs setup, put that setup in a concrete implementation with a no-argument constructor. The router can instantiate it by class name when it resolves the binding.
use App\Contracts\ExampleInterface;
use App\Services\ConfiguredExample;
use Luminova\Routing\DependencyResolver;
DependencyResolver::bind(ExampleInterface::class, ConfiguredExample::class);Use DependencyResolver::singleton() with the same class binding when each resolution should return the same instance.
Important Notes:
- Do not bind the same interface multiple times — only the last binding will take effect.
- If only one class exists, you can bind directly using the class name instead of an interface.
- Always use unique bindings so the DI system can resolve classes predictably.
Class Definition
- Full namespace:
Luminova\Routing\DependencyResolver
Constants
| Constant | Value | Meaning |
|---|---|---|
T_SINGLETON | 1 | A public static getInstance() method supplies the instance. |
T_INSTANTIATABLE | 2 | The class can be instantiated directly. |
Methods
bind
Register a concrete implementation for a dependency. Each resolution can create a new instance.
public static function bind(string $abstract, Closure|string $resolver): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$abstract | class-string | Fully qualified class or interface name. |
$resolver | Closure|class-string | Resolver argument. Use an instantiable implementation class for route injection. |
singleton
Register a shared implementation whose resolved instance is reused.
public static function singleton(string $abstract, Closure|string $resolver): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$abstract | class-string | Fully qualified class or interface name. |
$resolver | Closure|class-string | Resolver argument. Use an instantiable implementation class for route injection. |
resolve
Resolve a registered or default binding and create or retrieve its instance.
public static final function resolve(string $abstract): ?objectParameters:
| Parameter | Type | Description |
|---|---|---|
$abstract | class-string | Fully qualified class or interface name. |
Return:
?object - The resolved instance or null when the dependency cannot be resolved.
isInstantiable
Check whether a class can be constructed directly or through a public static getInstance() method.
public static function isInstantiable(
string $class,
?int &$mode = null,
?Throwable &$error = null
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$class | class-string | Class to inspect. |
$mode | ?int | Receives DependencyResolver::T_INSTANTIATABLE or DependencyResolver::T_SINGLETON on success. |
$error | ?Throwable | Receives the error on failure. |
Return:
bool - Whether the class can be instantiated.
Luminova's DI container provides helper methods to inspect and manage your bindings.These are handy when you need to verify if a dependency can be resolved, or to remove or replace a service during testing, debugging, or runtime overrides.
unbind
Removes a previously registered class or interface from the DI container.
public static function unbind(string $abstract): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$abstract | class-string | Fully qualified class or interface name. |
Example:
use App\Utils\Test;
use Luminova\Routing\DependencyResolver;
// Later, unbind it
DependencyResolver::unbind(Test::class);
// Now resolving will return null (unless a default mapping exists)
$service = DependencyResolver::resolve(Test::class); // null
- Safe to call even if the binding doesn't exist — it fails silently.
- Useful when swapping out implementations or cleaning up after tests.
- Default mappings (if any) are unaffected by unbinding.
has
Use has() when you just want to confirm that a class or interface can be resolved by the DI system — whether it's explicitly bound or automatically handled by default mappings.
public static function has(string $abstract): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$abstract | class-string | Fully qualified class or interface name. |
Return:
bool - Whether the dependency matches this lookup.
Example:
use Luminova\Routing\DependencyResolver;
if (DependencyResolver::has(Luminova\Components\Email\Mailer::class)) {
// Safe to inject or resolve MailService
$mail = DependencyResolver::resolve(Luminova\Components\Email\Mailer::class);
$mail?->address('[email protected]');
} else {
// Handle cases where the service isn't available
echo 'Mailer is not available in the DI container.';
}isBound
Use isBound() when you only care about manual bindings (via DependencyResolver::bind() or $this->bind() in your Application class). This does not consider any default mappings.
public static function isBound(string $abstract): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$abstract | class-string | Fully qualified class or interface name. |
Return:
bool - Whether the dependency matches this lookup.
Example:
use Luminova\Routing\DependencyResolver;
if (DependencyResolver::isBound(\App\Contracts\StorageInterface::class)) {
// Class was explicitly bound — we know exactly what implementation will be used
$storage = DependencyResolver::resolve(\App\Contracts\StorageInterface::class);
} else {
// Either not bound, or relying on defaults (if any)
echo 'StorageInterface is not explicitly registered.';
}