Download the PHP package michael-rubel/laravel-enhanced-container without Composer
On this page you can find all versions of the php package michael-rubel/laravel-enhanced-container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download michael-rubel/laravel-enhanced-container
More information about michael-rubel/laravel-enhanced-container
Files in michael-rubel/laravel-enhanced-container
Package laravel-enhanced-container
Short Description This package provides DX tweaks for Service Container in Laravel.
License MIT
Homepage https://github.com/michael-rubel/laravel-enhanced-container
Informations about the package laravel-enhanced-container
Laravel Enhanced Container
This package provides additional features for Laravel's Service Container.
StandWithUkraine
Contents
- Installation
- Usage
- Resolve contextual binding outside of constructor
- Method binding
- Method forwarding
Installation
Install the package via composer (Laravel 11):
If you need the package for older versions of Laravel, check changelog.
Usage
Resolve contextual binding outside of constructor
🔝 back to contents
Method binding
This feature makes it possible to override the behavior of methods accessed using the call
.
Assuming that is your function in the service class:
Bind the service to an interface:
Call your service method through container:
For example in feature tests:
Note: if you rely on interfaces, the proxy will automatically resolve bound implementation for you.
Note for package creators
If you want to use method binding in your own package, you need to make sure the LecServiceProvider
registered before you use this feature.
🔝 back to contents
Method forwarding
This feature automatically forwards the method if it doesn't exist in your class to the second one defined in the forwarding configuration.
You can define forwarding in your ServiceProvider:
You can as well use chained forwarding:
Important notes
- Pay attention to which internal instance you're now working on in
CallProxy
when using forwarding. The instance may change without your awareness. If you interact with the same methods/properties on a different instance, theInstanceInteractionException
will be thrown. - If you use
PHPStan/Larastan
you'll need to add the@method
docblock to the service to make it static-analyzable, otherwise it will return an error that the method doesn't exist in the class.
🔝 back to contents
Testing
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-enhanced-container with dependencies
illuminate/contracts Version ^11.21
spatie/laravel-package-tools Version ^1.9