PHP code example of elber / laravel-clean-architecture

1. Go to this page and download the library: Download elber/laravel-clean-architecture 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/ */

    

elber / laravel-clean-architecture example snippets


use Src\Billing\Application\Commands\CreateInvoice\{CreateInvoiceCommand, CreateInvoiceHandler};
use Src\Billing\Domain\Events\InvoiceCreatedEvent;
use Src\Billing\Infrastructure\InMemoryInvoiceWriteRepository;

test('creating an invoice persists it and records the creation event', function () {
    $repository = new InMemoryInvoiceWriteRepository;

    (new CreateInvoiceHandler($repository))->handle(new CreateInvoiceCommand('inv-1', []));

    $invoice = $repository->ofId('inv-1');
    expect($invoice)->not->toBeNull()
        ->and($invoice->releaseEvents()[0])->toBeInstanceOf(InvoiceCreatedEvent::class);
});

// config/clean-architecture.php
'id_type' => 'ulid',
bash
php artisan vendor:publish --tag=clean-architecture-config   # config file
php artisan vendor:publish --tag=clean-architecture-stubs    # customizable stubs
bash
php artisan clean:context Billing
php artisan clean:scaffold Billing Invoice   # add --id-type=ulid for ULID keys
bash
php artisan migrate
php artisan route:list --path=billing
bash
php artisan clean:test Billing Invoice --handler
php artisan clean:test Billing Invoice --feature
bash
# Constructor: string $id + array $data — Handler: Entity::create($command->id) + repository->save()
php artisan clean:command Billing CreateInvoice --entity=Invoice --crud=create

# Constructor: string $id + array $data — Handler: ofId() → throw NotFound → save()
php artisan clean:command Billing UpdateInvoice --entity=Invoice --crud=update

# Constructor: string $id — Handler: ofId() guard → repository->delete($command->id)
php artisan clean:command Billing DeleteInvoice --entity=Invoice --crud=delete
bash
# UUIDv7 keys — the default
php artisan clean:scaffold Billing Invoice

# ULID keys
php artisan clean:scaffold Billing Invoice --id-type=ulid
bash
php artisan clean:context Billing                  # generates api.php (default)
php artisan clean:context Billing --routes=web     # generates web.php
php artisan clean:context Billing --routes=both    # generates api.php + web.php

src/{Context}/Infrastructure/{Context}ServiceProvider.php
bash
php artisan vendor:publish --tag=clean-architecture-stubs