How saas-laravel/laravel-cqrs works
Main Components
• HTTP / Presentation Layer – Represented by the “app/Http” folder, including controllers (for example, UserController) and middleware.
• Command & Query Bus Layer – Found in “app/Bus”. You have CommandBus, QueryBus, and their implementations (IlluminateCommandBus, IlluminateQueryBus). This is the core orchestration component that routes requests to the right handlers.
• Domain and Modules – Under “src/Modules”, there are several domain modules such as User, Ai, and FeatureFlag. Each module is further divided into sub-components:
– Commands (for creating or updating domain entities, e.g., CreateUserCommand and its handler)
– Queries (for read operations such as FindUserQuery and its handler)
– Service layers (e.g., AiService, FeatureFlagService) that enforce business logic
– Repository patterns for data persistence (both read and write repositories in the User module)
• Core Components – The “src/Core” directory contains shared elements like exceptions which support error management across modules.
• Infrastructure & External Services – The “src/Modules/Ai/Client” (with implementations like AiClient and OpenAiClient) and “src/Modules/FeatureFlag/Clients” indicate external service integrations.
Data Flows and Interactions
• Controllers translate these requests into Commands or Queries which are dispatched via the Command Bus/Query Bus layer.
• The Command/Query Bus then forwards the request to the appropriate handler (located within the domain modules in “src/Modules/User/Commands” or “src/Modules/User/Queries”).
• Handlers may interact with the service layer, which further uses repositories (read/write) to interact with the database.
• For actions related to AI or feature flagging, the corresponding modules delegate processing to external API clients (such as OpenAiClient or GrowthbookFeatureFlagClient).
Architectural Patterns and Design Principles
• Separation of Concerns – Clear distinctions are made between controllers, command/query handling, and domain logic.
• CQRS – The creation of separate commands and queries ensures that write and read operations are handled independently.
• Dependency Injection – While not explicitly detailed in the file tree, Laravel’s service container (supported by “app/Providers”) facilitates loosely-coupled dependencies.
• Repository Pattern – Under the User module, separate contracts and repository implementations (both read and write) are present, indicating abstractions over data access.
• Modular Design – With modules for AI, feature flags, and user management, the design promotes a high degree of encapsulation and reusability.
• Presentation layer (controllers, middleware)
• Orchestration layer (command and query buses)
• Domain/Business logic (modules with commands, queries, services)
• Infrastructure dependencies (repositories, external API clients)
• Annotations indicating key architectural patterns such as CQRS, repository pattern, and dependency injection.