1. Class Resolution Injection
class PaymentService {
public function __construct(
protected Gateway $gateway,
protected Logger $logger
) {}
}
// Automatically resolves Gateway and Logger via reflection or autowiring:cache
$service = \Illuminate\Container\Container::getInstance()
->make(PaymentService::class);
2. Method Injection via BoundMethod::call
class OrderAction {
public function execute(int $id, ApiClient $client) {
// $client is injected automatically
}
}
$action = \Illuminate\Container\Container::getInstance()
->make(\App\Actions\OrderAction::class);
\Illuminate\Container\BoundMethod::call(
\Illuminate\Container\Container::getInstance(),
[$action, 'execute'],
['id' => 99] // associative array is needed for bound call to use the autowiring cache
);
3. The Performance Layer: autowiring:cache
Reflection is slow. Maravel bypasses it by caching these paths. Run php artisan autowiring:cache to generate a static map in bootstrap/cache/autowiring.php.
To include your custom directories, update your config:
/**
* artisan autowiring:cache source paths for public methods (except __construct which is implicitly handled)
* The CallQueuedHandler, controllers, middlewares, built-in commands, service providers, macroable classes
* + other classes resolved from Container during the autowiring:cache command execution are handled automatically
* 'path' can be a single class or a directory
* This allows you to add autowiring to any constructor/method from a class if you want
* when you resolve that class from the container or use BoundMethod::call to call that method.
*/
'autowiring' => [
[
'path' => \app()->path() . DIRECTORY_SEPARATOR . 'Console' . DIRECTORY_SEPARATOR . 'Commands',
'methods' => ['handle', '__invoke'],
],
[
'path' => \app()->path() . DIRECTORY_SEPARATOR . 'Jobs',
'methods' => ['handle', '__invoke'],
],
[
'path' => \app()->path() . DIRECTORY_SEPARATOR . 'Http' . DIRECTORY_SEPARATOR . 'Requests',
'methods' => ['validator', 'authorize', 'after', 'rules'],
],
[
'path' => \app()->path() . DIRECTORY_SEPARATOR . 'Listeners',
'methods' => [],
],
[
'path' => \app()->path() . DIRECTORY_SEPARATOR . 'Actions',
'methods' => ['execute'],
],
],
NOTE: Optimizing Primitive Parameters
When resolving a class from the container that has primitive parameters in its __construct, use an associative array if you are not providing every single required parameter.
class PaymentService {
public function __construct(
int $id,
protected Gateway $gateway,
protected Logger $logger
) {}
}
// Automatically resolves Gateway and Logger via autowiring:cache
$service = \Illuminate\Container\Container::getInstance()
->make(PaymentService::class, ['id' => 99]);
Warning: Maravel also supports lists as arguments when resolving a class, but if you do not pass all parameters in that list, the framework must fall back to Reflection to resolve the missing ones. It is best to always use associative arrays for partial parameters for classes included in the autowiring cache.

Top comments (0)