Vertical Slice Architecture is a software architecture pattern that organizes code around single features or functionalities. This approach differs from traditional layered architecture, which organizes code into layers based on functionality or responsibility.
Vertical Slice Architecture offers several benefits, including:
- Modularity and Flexibility: It comprises self-contained code units that can be independently developed and deployed. This simplifies adding new features or altering existing ones without impacting the rest of the application.
- Testability: It's easier to test compared to traditional layered applications. Each slice focuses on a single functionality, making it simpler to identify and isolate the code for testing.
- Maintainability: It's easier to maintain than traditional layered applications. Each slice concentrates on a single functionality, simplifying code understanding and troubleshooting.
Let's envision a use case employing Vertical Slices within a Yii2 developed blog. For this scenario, i'll utilize Yii2 inline actions. Essentially, i'll create an action for each specific function, enabling me to maintain lean controllers with modular and isolated actions.
Yii2 inline actions allow for a more granular approach, aiding in the creation of streamlined controllers by compartmentalizing functionalities. By having distinct actions dedicated to specific tasks, the codebase becomes more modular and manageable.
Here is the link to Yii2 documentation on inline actions for further reference.
Example of Yii2 Blog Structure:
src
├── Domains
│ ├── Category
│ ├── Post
│ ├── Seo
│ ├── Tag
├── Framework
│ ├── Asset
│ ├── Behavior
│ ├── EventHandler
│ ├── Migration
│ ├── resource
│ ├── UseCase
├── Category
│ ├── Delete
│ ├── Disable
│ ├── Enable
│ ├── Index
│ ├── Register
│ ├── Update
│ ├── view
│ ├── CategoryController.php
│ ├── CategoryEvent.php
│ ├── CategoryForm.php
│ ├── CategoryEvent.php
In Yii3, the handling of actions directly, as opposed to predefined controllers in Yii2, fosters a more granular and modular design approach. This shift allows for greater flexibility and a finer level of control over individual actions within the application. Let's delve into an example involving a user extension to further demonstrate this aspect.
Example of Yii3 User Extension Structure:
src
├── Domain
│ ├── Account
│ ├── Identity
│ ├── Profile
├── Framework
│ ├── Asset
│ ├── Miration
│ ├── Repository
│ ├── resource
├── UseCase
│ ├── AdminManagement
│ ├── BlockByAdmin
│ ├── ChangePasswordByAdmin
│ ├── Confirmation
│ ├── ConfirmationByAdmin
│ ├── DeleteByAdmin
│ ├── EmailChange
│ ├── EmailStrategy
│ ├── Google2FA
│ │ ├── view
│ │ │ ├── google2fa.php
│ │ ├── Google2FAAction.php
│ │ ├── Google2FAFormModel.php
│ ├── GoogleAuth
│ ├── Login
│ ├── Logout
│ ├── PasswordRequest
│ ├── PasswordReset
│ ├── ProfileUpdate
│ ├── Register
│ ├── ResendConfirmation
│ ├── UnblockByAdmin
│ ├── UpdateByAdmin
Vertical Slice is a powerful software architecture pattern that can help you to build more modular, flexible, and maintainable applications. If you are looking for a way to improve the architecture of your PHP applications, Vertical Slice is a great option to consider.
Structuring by Use Case with Vertical Slices - Yii Documentation.
For the foundational content of my article, i drew inspiration from the following resource on structuring code by use case with Vertical Slices.
I extend a special thanks to Alexander Makarov for showcasing these best practices, which have been instrumental in understanding and illustrating the concept of Vertical Slices in software architecture.
Happy coding!