home / skills / hoangnguyen0403 / agent-skills-standard / clean-architecture
This skill guides Laravel Clean Architecture adoption by organizing domains, DTOs, and ports for maintainable, testable code.
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill clean-architectureReview the files below or copy the command above to add this skill to your agents.
---
name: Laravel Clean Architecture
description: Expert patterns for DDD, DTOs, and Ports & Adapters in Laravel.
metadata:
labels: [laravel, ddd, architecture, solid, clean-code]
triggers:
files: ['app/Domains/**/*.php', 'app/Providers/*.php']
keywords: [domain, dto, repository, contract, adapter]
---
# Laravel Clean Architecture
## **Priority: P1 (HIGH)**
## Structure
```text
app/
├── Domains/ # Logic grouped by business domain
│ └── {Domain}/
│ ├── Actions/ # Single use-case logic
│ ├── DTOs/ # Immutable data structures
│ └── Contracts/ # Interfaces for decoupling
└── Providers/ # Dependency bindings
```
## Implementation Guidelines
- **Domain Grouping**: Organize code by business domain (e.g., `User`, `Order`) instead of framework types.
- **DTOs**: Use `readonly` classes to pass data between layers; avoid raw arrays.
- **Action Classes**: Wrap business logic in single-purpose classes with `handle()` or `execute()`.
- **Repository Pattern**: Abstract Eloquent queries behind interfaces for easier testing.
- **Dependency Inversion**: Bind Interfaces to implementations in `AppServiceProvider`.
- **Model Isolation**: Keep Eloquent models lean; only include relationships and casts.
## Anti-Patterns
- **Domain Leak**: **No Eloquent in Controllers**: Use DTOs/Actions to bridge layers.
- **Array Overload**: **No raw data arrays**: Use typed DTOs for structured data.
- **Service Bloat**: **No God Services**: Break down large services into granular Actions.
- **Infrastructure Coupling**: **No hard dependencies**: Depend on abstractions, not concretions.
## References
- [DDD & Repository Patterns](references/implementation.md)
This skill codifies Laravel Clean Architecture patterns for Domain-Driven Design, DTOs, and Ports & Adapters to build maintainable, testable applications. It prescribes a domain-first folder layout, immutable data transfer objects, single-purpose action classes, and interface-based bindings. The guidance reduces framework coupling and keeps Eloquent models focused on persistence concerns.
The skill inspects and enforces a domain-oriented structure under app/Domains plus dependency bindings in Providers. It promotes readonly DTO classes for cross-layer data, Action classes (handle/execute) for single use-cases, and repository interfaces that hide Eloquent queries behind ports. It highlights anti-patterns like using Eloquent in controllers, raw arrays for domain data, god services, and direct infrastructure coupling, then recommends concrete replacements.
How do I bind interfaces to implementations?
Bind repository and other interfaces in your Providers (e.g., AppServiceProvider) using the container so domain code depends on abstractions, not concretes.
Should DTOs be classes or arrays?
Prefer readonly, typed DTO classes to enforce structure and immutability; avoid raw arrays which leak implicit contracts and make testing brittle.
Where should validation happen?
Perform transport-level validation in form requests or controllers, map validated data into DTOs, and let Actions assume well-formed DTOs for business logic.