PHP code example of lunarstorm / laravel-ddd
1. Go to this page and download the library: Download lunarstorm/laravel-ddd 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/ */
lunarstorm / laravel-ddd example snippets
// In config/ddd.php
'application_path' => 'app/Modules',
'application_namespace' => 'App\Modules',
'application_objects' => [
'controller',
'request',
'middleware',
],
// In config/ddd.php
'layers' => [
'Infrastructure' => 'src/Infrastructure',
],
use Tey\LaravelDDD\Facades\DDD;
use Tey\LaravelDDD\ValueObjects\CommandContext;
use Tey\LaravelDDD\ValueObjects\ObjectSchema;
DDD::resolveObjectSchemaUsing(function (string $domainName, string $nameInput, string $type, CommandContext $command): ?ObjectSchema {
if ($type === 'controller' && $command->option('api')) {
return new ObjectSchema(
name: $name = str($nameInput)->replaceEnd('Controller', '')->finish('ApiController')->toString(),
namespace: "App\\Api\\Controllers\\{$domainName}",
fullyQualifiedName: "App\\Api\\Controllers\\{$domainName}\\{$name}",
path: "app/Api/Controllers/{$domainName}/{$name}.php",
);
}
// Return null to fall back to the default
return null;
});
'autoload' => [
'providers' => true,
'commands' => true,
'policies' => true,
'factories' => true,
'migrations' => true,
'listeners' => false,
],
namespace Domain\Invoicing\Listeners;
use Domain\Invoicing\Events\InvoiceCreated;
class SendInvoiceNotification
{
public function handle(InvoiceCreated $event): void
{
// Send the notification.
}
}
'autoload_ignore' => [
'Tests',
'Database/Migrations',
],
use Tey\LaravelDDD\Facades\DDD;
use Symfony\Component\Finder\SplFileInfo;
DDD::filterAutoloadPathsUsing(function (SplFileInfo $file) {
if (basename($file->getRelativePathname()) === 'functions.php') {
return false;
}
});
'autoload' => [
'providers' => false,
'commands' => false,
'policies' => false,
'factories' => false,
'migrations' => false,
'listeners' => false,
],
bash
php artisan ddd:install
bash
php artisan ddd:config wizard
bash
php artisan ddd:config update
bash
php artisan ddd:model Invoicing:Invoice
bash
php artisan help ddd:model
php artisan help ddd:controller
bash
php artisan boost:install
bash
php artisan ddd:config
bash
php artisan ddd:model Invoicing:Invoice
php artisan ddd:controller Invoicing:InvoiceController --model=Invoice --requests --api
├─ app
| └─ Modules
│ └─ Invoicing
│ ├─ Controllers
│ │ └─ InvoiceController.php
│ └─ Requests
│ ├─ StoreInvoiceRequest.php
│ └─ UpdateInvoiceRequest.php
├─ src/Domain
└─ Invoicing
└─ Models
└─ Invoice.php
bash
php artisan ddd:model Invoicing:Invoice
php artisan ddd:trait Infrastructure:Concerns/HasExpiryDate
├─ src/Domain
| └─ Invoicing
| └─ Models
| └─ Invoice.php
├─ src/Infrastructure
└─ Concerns
└─ HasExpiryDate.php
bash
# Sync composer.json with ddd.php
php artisan ddd:config composer
bash
php artisan ddd:model Invoicing:Payment/Transaction
# -> Domain\Invoicing\Models\Payment\Transaction
php artisan ddd:action Invoicing:Payment/ProcessTransaction
# -> Domain\Invoicing\Actions\Payment\ProcessTransaction
php artisan ddd:exception Invoicing:Payment/PaymentFailedException
# -> Domain\Invoicing\Exceptions\Payment\PaymentFailedException
bash
php artisan ddd:class Invoicing:Support/InvoiceBuilder
# -> Domain\Invoicing\Support\InvoiceBuilder
php artisan ddd:interface Invoicing:Contracts/PayableByCreditCard
# -> Domain\Invoicing\Contracts\PayableByCreditCard
php artisan ddd:trait Invoicing:Models/Concerns/HasLineItems
# -> Domain\Invoicing\Models\Concerns\HasLineItems
bash
# The usual: generate a provider in the configured provider namespace
php artisan ddd:provider Invoicing:InvoiceServiceProvider
# -> Domain\Invoicing\Providers\InvoiceServiceProvider
# Override the configured namespace at runtime
php artisan ddd:provider Invoicing:/InvoiceServiceProvider
# -> Domain\Invoicing\InvoiceServiceProvider
# Generate an event inside the Models namespace (hypothetical)
php artisan ddd:event Invoicing:/Models/EventDoesNotBelongHere
# -> Domain\Invoicing\Models\EventDoesNotBelongHere
# Deep nesting is supported
php artisan ddd:exception Invoicing:/Models/Exceptions/InvoiceNotFoundException
# -> Domain\Invoicing\Models\Exceptions\InvoiceNotFoundException
bash
php artisan ddd:controller Invoicing:PaymentController --api
# Controller [app/Api/Controllers/Invoicing/PaymentApiController.php] created successfully.
bash
php artisan ddd:stub --list
bash
php artisan ddd:stub
bash
php artisan ddd:stub listener.