aldemeery/onion
Aldemeery Onion is a Laravel package for adding “onion” (layered) architecture patterns to your app, helping you organize code into clear domain, application, and infrastructure layers with cleaner boundaries, structure, and maintainability.
app/Http/Controllers, app/Services) is not strictly Onion, the package can refactor existing code into a more scalable structure without breaking Laravel’s ecosystem (e.g., via Facades, Service Providers, or custom bindings).User module) before full migration. This reduces risk and allows for gradual refactoring.Infrastructure) may need adjustments for Laravel’s middleware pipeline. Solution: Use kernel middleware or route model binding to adapt.Infrastructure layer can wrap repositories, but raw queries may require custom adapters.Infrastructure without Laravel’s bootstrapping).| Risk Area | Severity | Mitigation |
|---|---|---|
| Breaking Changes | Medium | Start with a proof-of-concept in a non-critical module. |
| Laravel-Specific Overhead | High | Requires custom Service Providers or Facade wrappers for seamless integration. |
| Performance Overhead | Low | Minimal if layers are optimized (e.g., caching domain objects). |
| Learning Curve | Medium | Team may need training on DDD/Onion principles and PHP DI. |
| Vendor Lock-in | Low | MIT license + open-source; no proprietary dependencies. |
app/Http for APIs while layering domains) may be optimal.Infrastructure in unit tests).Infrastructure dependencies (e.g., Database, Cache).DomainEvent → Infrastructure handler).| Phase | Actions | Tools/Strategies |
|---|---|---|
| Assessment | Audit existing codebase for tight coupling and framework dependencies. | Static analysis (PHPStan, Psalm), architecture diagrams. |
| Pilot Module | Refactor one domain (e.g., Order) into Onion layers. |
Feature flags, branch-by-abstraction. |
| Dependency Mapping | Identify cross-layer dependencies (e.g., Controllers calling Domain directly). |
Dependency injection graphs (e.g., php-dependency-graph). |
| Service Provider Setup | Register domain interfaces in Laravel’s container. | Custom AppServiceProvider bindings. |
| Facade/Adapter Layer | Create Laravel-compatible wrappers for Infrastructure (e.g., DatabaseRepository). |
Facades, decorators. |
| Testing Overhaul | Rewrite tests to mock Infrastructure and test Domain in isolation. |
PHPUnit, Pest, Mockery. |
| Full Migration | Gradually replace Controllers/Services with layered architecture. | Incremental feature delivery. |
Infrastructure layer can handle dispatching.Infrastructure for serialization.Application layer).Infrastructure repositories for resolution.Notification domain.Domain logic (entities, use cases).Application services (commands, DTOs).Infrastructure (repositories, external APIs).Application-layer handlers (e.g., CommandBus).Infrastructure (e.g., database schema) without touching Domain.laravel/framework) are confined to Infrastructure.php-di integration).Application layer.Domain → Infrastructure latency).rector) can enforce rules.config/ structure for layered apps.Domain/Application: Easy to scale (e.g., queue workers).Infrastructure: Database connections, caches must be managed (e.g., Laravel Horizon for queues).Application layer to define DTOs for inter-service communication.Infrastructure repositories can route queries accordingly.Domain (commands) from Infrastructure (queries).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Infrastructure Outage | Domain logic fails if blocked. |
Implement retries (e.g., Laravel’s retry helper) and **fall |
How can I help you explore Laravel packages today?