Blog Detail

04

Jul
Binds interfaces to implementations automatically in Laravel cover image

arrow_back Binds interfaces to implementations automatically in Laravel

Laravel Auto-Binder is an awesome package that adds the possibility to bind interfaces to implementations in the Service Container by scanning the specified project folders. This helps avoid manually registering container bindings when the project needs to bind a lot of interfaces to its implementations. The package requires PHP 8.x and Laravel 9.x.

Installation

Install the package using composer:

composer require michael-rubel/laravel-auto-binder

Usage

Define in your ServiceProvider:

AutoBinder::from(folder: 'Services')
    ->as('singleton')
    ->bind();

Assuming you have your services in the App\Services and its interfaces in the App\Services\Interfaces, the package will register binding for each pair of class and interface:

$this->app->singleton(AuthServiceInterface::class, AuthService::class);
$this->app->singleton(UserServiceInterface::class, UserService::class);
$this->app->singleton(CompanyServiceInterface::class, CompanyService::class);

Customization

If you need to customize the base path or
namespace, you can use the following methods:

AutoBinder::from(folder: 'Services')
    ->basePath('app/Domain')
    ->classNamespace('App\\Domain')
    ->interfaceNamespace('App\\Domain\\Interfaces')
    ->bind();

This configuration would look for classes in the app/Domain/Services folder, use App\Domain namespace and apply interfaces from App\Domain\Interfaces namespace with ClassNameInterface naming convention.

If you need to change the naming convention of your interfaces, you can specify the namespace and name you prefer:

AutoBinder::from(folder: 'Services')
    ->interfaceNaming('Contract')
    ->bind();

This configuration scans the app/Services folder with App\Services namespace, App\Services\Contracts interface namespace and ClassNameContract interface naming convention.

Excluding subfolders from scan

You might as well exclude subdirectories from the scan of the root directory:

AutoBinder::from(folder: 'Services')
    ->exclude('Traits', 'Components')
    ->bind();

Scanning multiple folders at once

If you pass multiple folders, `from the method will return an instance of Illuminate/Support/Collection. Assuming that, you can loop over your AutoBinder class instances with access to internal properties.

For example:

AutoBinder::from('Services', 'Models')->each(
    fn ($binder) => $binder->basePath('app')
        ->classNamespace('App\\Domain')
        ->interfaceNamespace("App\\Domain\\$binder->classFolder\\Interfaces")
        ->as('singleton')
        ->bind()
);

For more details, you can visit its documentation and source code on Github.

Closing Notes

If you want to develop any custom web App, CRM Software, SAAS App, or eCommerce application then Codebrisk is here to help you. We have built an exquisite team of expert Laravel developers who share our passion. Our combined knowledge & experience ensure smooth onboarding, a quick start, and satisfying results. We covers both small and large IT projects according to your needs. We are open to any type of contract and are always happy to explain any complexities. So if you have a great idea, please feel free to contact us or start a project with us.

Published at : 04-07-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

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 your project

Launch project