plin-code / laravel-clean-architecture
Laravel package for generating Clean Architecture structure
dev-main
2025-05-30 08:04 UTC
Requires
- php: ^8.3
- illuminate/console: ^12.0
- illuminate/filesystem: ^12.0
- illuminate/support: ^12.0
Requires (Dev)
- larastan/larastan: ^3.4.0
- laravel/pint: ^1.13
- mockery/mockery: ^1.6
- orchestra/testbench: ^10.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
This package is not auto-updated.
Last update: 2025-06-13 17:54:03 UTC
README
A Laravel package to easily implement Clean Architecture in your projects. ๐
โจ Features
- ๐ฏ 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
๐ Requirements
- ๐ PHP 8.3+
- โก Laravel 12.x
๐ฆ Installation
composer require plin-code/laravel-clean-architecture
โ๏ธ Configuration
Publish the configuration files and stubs:
php artisan vendor:publish --provider="PlinCode\LaravelCleanArchitecture\CleanArchitectureServiceProvider"
๐ฏ Usage
๐๏ธ Installing Clean Architecture structure
php artisan clean-arch:install
This command will create:
- ๐ Folder structure for Domain, Application and Infrastructure layers
- ๐งฉ Base classes (BaseModel, BaseAction, BaseService, etc.)
- โ๏ธ Configuration file
- ๐ Documentation
๐ Creating a new domain
php artisan clean-arch:make-domain User
This 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
- ๐งช Feature tests
๐ ๏ธ Available commands
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:generate-package {name} {vendor}
- ๐ฆ Generate a new package
๐ Generated structure
app/
โโโ Application/
โ โโโ Actions/
โ โ โโโ Users/
โ โ โโโ CreateUserAction.php
โ โ โโโ UpdateUserAction.php
โ โ โโโ DeleteUserAction.php
โ โ โโโ GetByIdUserAction.php
โ โโโ Services/
โ โโโ UserService.php
โโโ Domain/
โ โโโ Users/
โ โโโ User.php
โ โโโ Enums/
โ โ โโโ UserStatus.php
โ โโโ Events/
โ โโโ UserCreated.php
โ โโโ UserUpdated.php
โ โโโ UserDeleted.php
โโโ Infrastructure/
โโโ API/
โโโ Controllers/
โ โโโ UsersController.php
โโโ Requests/
โ โโโ CreateUserRequest.php
โ โโโ UpdateUserRequest.php
โโโ Resources/
โโโ UserResource.php
๐๏ธ Clean Architecture Principles
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.)
๐ Dependencies
- ๐ฏ Domain Layer: Does not depend on any other layer
- โก Application Layer: Depends only on Domain Layer
- ๐๏ธ Infrastructure Layer: Depends on Application and Domain Layers
๐ก Examples
๐๏ธ Creating a Product domain
php artisan clean-arch:make-domain Product
๐ฎ Using in controller
class 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); } }
โ๏ธ Configuration
The configuration file config/clean-architecture.php
allows you to customize:
- ๐ท๏ธ Default namespace
- ๐ Directory paths
- โ Validation options
- ๐ Logging settings
๐ ๏ธ Development
This package uses several tools to maintain code quality:
๐ง Code Quality Tools
- ๐จ 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
๐ Available Scripts
# ๐งช 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
๐ Development Setup
- ๐ฅ Clone the repository
- ๐ฆ Install dependencies:
composer install
- โจ Run quality checks:
composer quality
๐ค Contributing
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. ๐
๐ License
MIT ๐