20
JanMichael Rubel has introduced a package called Laravel Auto Binder, which can automatically bind interfaces to implementations in Service Container, scanning the specified project folders. This can assist you in avoiding manually registering container bindings when the project needs to bind a lot of interfaces to its implementations without any further dependencies.
One requirement you should follow to utilize
this package is (Folder with interfaces always should be the child of the folder where it belongs). For example: App\Services\YourService => App\Services\Interfaces\YourServiceInterface
. You can customize folders to scan, type of bindings, and the naming convention of your interfaces in the config. The package requires PHP ^8.x
and Laravel ^8.71
.
You can install this package using composer by running this command:
composer require michael-rubel/laravel-auto-binder
Then publish and customize the configuration:
php artisan vendor:publish --tag="auto-binder-config"
You can edit the config and put your classes and interfaces in the proper folders with proper class naming. That’s all.
So, this kind of bindings:
$this->app->bind(AuthServiceInterface::class, AuthService::class);
$this->app->bind(UserServiceInterface::class, UserService::class);
$this->app->bind(CompanyServiceInterface::class, CompanyService::class);
Or provider’s array:
public array $singletons = [
AuthServiceInterface::class => AuthService::class,
UserServiceInterface::class => UserService::class,
CompanyServiceInterface::class => CompanyService::class,
];
It will be registered automatically for you.
For more detailed documentation & source code you can visit Github.
Published at : 20-01-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project