PHP code example of michael-rubel / laravel-auto-binder

1. Go to this page and download the library: Download michael-rubel/laravel-auto-binder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

michael-rubel / laravel-auto-binder example snippets


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

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

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

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

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

AutoBinder::from(folder: 'Services')
    ->when(ExampleServiceInterface::class, function ($app, $service) {
        return new ExampleService($app);
    })
    ->bind();

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

AutoBinder::from(folder: 'Services')
    ->withoutCaching()
    ->as('singleton')
    ->bind();
shell
php artisan binder:clear Services,Models