A Laravel package to easily implement Clean Architecture in your projects. ๐
- ๐ฏ Domain-Driven Design - Organize your code with DDD principles
- โก Quick Setup - Get started with Clean Architecture in minutes
- ๐งฉ Auto-Generation - Generate complete domains with one command
- ๐๏ธ Layer Separation - Clear separation between Domain, Application, and Infrastructure
- ๐ง Customizable - Flexible configuration to fit your project needs
- ๐งช Test-Ready - Pre-built test templates for immediate testing
- ๐ Well-Documented - Comprehensive documentation and examples
- ๐จ Modern PHP - Built for PHP 8.3+ with latest Laravel features
- ๐ PHP 8.3+
- โก Laravel 12.x / 13.x
composer require plin-code/laravel-clean-architecturePublish the configuration files and stubs:
php artisan vendor:publish --provider="PlinCode\LaravelCleanArchitecture\CleanArchitectureServiceProvider"php artisan clean-arch:installThis command will create:
- ๐ Folder structure for Domain, Application and Infrastructure layers
- ๐งฉ Base classes (BaseModel, BaseAction, BaseService, etc.)
- โ๏ธ Configuration file
- ๐ Documentation
php artisan clean-arch:make-domain UserThis command will generate:
- ๐๏ธ Domain model with events
- ๐ Status enums
- ๐ Domain events (Created, Updated, Deleted)
- โก Actions (Create, Update, Delete, GetById)
- ๐ง Service
- ๐ API Controller
- ๐ Form Requests (Create, Update)
- ๐ค API Resource
- ๐๏ธ Database migration
- ๐งช Feature tests
After generating the core files, make-domain prompts interactively for optional components. You can choose to also generate an Observer, Listener, Job, Mail, Notification, and Export for the domain. Each prompt can be answered independently, so you only generate what your domain needs.
php artisan clean-arch:validateThis command checks your codebase for layer dependency violations (for example, Domain code importing from Infrastructure). It returns exit code 1 when violations are found, making it suitable for use in CI pipelines.
Clean Architecture Validation
=============================
โ Domain has no Application imports
โ Domain has no Infrastructure imports
โ Application has no Infrastructure imports
โ No Observers in Domain
โ No Jobs in Infrastructure
โ No Commands in Infrastructure
โ No duplicate Services directory
No violations found.
clean-arch:install- ๐๏ธ Install Clean Architecture structureclean-arch:make-domain {name}- ๐ Create a complete new domainclean-arch:make-action {name} {domain}- โก Create a new actionclean-arch:make-service {name}- ๐ง Create a new serviceclean-arch:make-controller {name}- ๐ Create a new controllerclean-arch:make-observer {name} {domain}- ๐๏ธ Create a new observerclean-arch:make-listener {name}- ๐ Create a new listenerclean-arch:make-job {name}- โณ Create a new jobclean-arch:make-mail {name}- ๐ง Create a new mailableclean-arch:make-notification {name}- ๐ Create a new notificationclean-arch:make-export {name}- ๐ค Create a new exportclean-arch:validate- โ Validate architecture dependency rulesclean-arch:generate-package {name} {vendor}- ๐ฆ Generate a new package
app/
โโโ Domain/ # Pure business logic
โโโ Application/ # Use cases and orchestration
โ โโโ Actions/
โ โโโ Services/
โ โโโ Jobs/
โ โโโ Listeners/
โ โโโ Console/Commands/
โโโ Infrastructure/ # Framework adapters
โโโ Http/
โ โโโ Controllers/Api/
โ โโโ Middleware/
โ โโโ Requests/
โ โโโ Resources/
โโโ UI/
โโโ Mail/
โโโ Notifications/
โโโ Observers/
โโโ Exports/
โโโ Validation/
โโโ Exceptions/
app/
โโโ Domain/
โ โโโ Users/
โ โโโ Models/
โ โ โโโ User.php
โ โโโ Enums/
โ โ โโโ UserStatus.php
โ โโโ Events/
โ โโโ UserCreated.php
โ โโโ UserUpdated.php
โ โโโ UserDeleted.php
โโโ Application/
โ โโโ Actions/
โ โ โโโ Users/
โ โ โโโ CreateUserAction.php
โ โ โโโ UpdateUserAction.php
โ โ โโโ DeleteUserAction.php
โ โ โโโ GetByIdUserAction.php
โ โโโ Services/
โ โโโ UserService.php
โโโ Infrastructure/
โโโ Http/
โโโ Controllers/
โ โโโ Api/
โ โโโ UsersController.php
โโโ Requests/
โ โโโ CreateUserRequest.php
โ โโโ UpdateUserRequest.php
โโโ Resources/
โโโ UserResource.php
This package implements Clean Architecture principles:
- ๐ฏ Domain Layer: Contains business logic and entities
- โก Application Layer: Contains use cases and application logic
- ๐๏ธ Infrastructure Layer: Contains implementation details (controllers, database, etc.)
- ๐ฏ Domain Layer: Does not depend on any other layer
- โก Application Layer: Depends only on Domain Layer
- ๐๏ธ Infrastructure Layer: Depends on Application and Domain Layers
php artisan clean-arch:make-domain Productclass ProductsController extends Controller
{
public function __construct(
private CreateProductAction $createProductAction,
private ProductService $productService
) {}
public function store(CreateProductRequest $request): JsonResponse
{
$product = $this->createProductAction->execute($request);
return response()->json([
'data' => new ProductResource($product),
'message' => 'Product created successfully'
], 201);
}
}The configuration file config/clean-architecture.php allows you to customize:
- ๐ท๏ธ Default namespace
- ๐ Directory paths
- โ Validation options
- ๐ Logging settings
This package uses several tools to maintain code quality:
- ๐จ Laravel Pint - Code formatting and style fixing
- ๐ PHPStan - Static analysis for finding bugs
- ๐งช PEST - Modern testing framework built on PHPUnit
- ๐ญ Orchestra Testbench - Laravel package testing
# ๐งช Run tests
composer test
# ๐ Run tests with coverage
composer test-coverage
# ๐จ Fix code style
composer format
# ๐ Check code style without fixing
composer format-test
# ๐ Run static analysis
composer analyse
# โจ Run all quality checks
composer quality- ๐ฅ Clone the repository
- ๐ฆ Install dependencies:
composer install - โจ Run quality checks:
composer quality
Pull requests are welcome! ๐ For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate and follow our Contributing Guidelines. ๐
MIT ๐