Layers
You can think of three layers for setting up your application.
- Framework layer
- Domain layer
- Application layer;
- Infrastructure layer;
Framework
The Framework layer sits outside of the Application layer. It contains code that your application uses but it is not actually your application. This is often literally your framework, but can also include any third-party libraries, SDKs or other code used. The framework layer implements services defined by the application layer. For example, it might implement a notification interface to send emails or SMS. The application knows it needs to send notifications, but it may not need to care how they are sent (email vs SMS for example).
Each layer defines how other layers can communicate with eachother. At the layer boundary we find interfaces. These are ports for the next layer to create adapters for.
Domain
Your domain layer will contain all objects that define the domain. The models you create live in this layer, as do the interfaces/ports to retrieve data regarding to the models. The exceptions that can be thrown by the interface implementors well also reside in this layer.
Application
Although arguable, it is quite common to store all actions in de application layer that can be taken on the models from the domain, since actions are what your application actually is. Working in DDD (Domain Driven Development) and CQRS (Command Query Responsibility Segregation) these will be typically be Commands en Queries, as well as the Events that will be triggered after Commands are executed.
Infrastructure
The infrastructure layer will contain all the actual implementations/adapters for the interfaces/ports that are mentioned. For instance, if we have a Post model, with a PostRepositoryInterface defined in our Domain layer, we'll implement the repository here.
Example of folder-structure
- src/Domain/Post/Model/Post.php
- src/Domain/Post/Exception/PostException.php
- src/Domain/Repository/PostRepositoryInterface.php
- src/Application/Command/CommandBusInterface.php
- src/Application/Command/CommandInterface.php
- src/Application/Command/CommandHandlerInterface.php
- src/Application/Command/Post/CreatePostCommand.php
- src/Application/Command/Post/Handler/CreatePostHandler.php
- src/Infrastructure/CommandBus/SynchronousCommandBus.php
- src/Infrastructure/Persistence/PostRepository.php