Comparing
#80 with
#89
Content
[...]
>
> [Yii v2 snippet guide, part 1](https://www.yiiframework.com/wiki/2552/yii-v2-snippet-guide)
>
> [Yii 2 Basic Project, Git](https://gitlab.com/radin.cerny/yii2-app-basic-plus)
## Demo applications
Yii3 offers more demo applications, but their list is a bit confusing.
In the [Yii3 Guide](https://yiisoft.github.io/docs/guide/start/demo-apps.html) there now are mentioned 3 demos, plus there are other 3 templates. Yii team makes a difference between word Demo and Template. Templates are simpler:
- [Web App](https://github.com/yiisoft/app)
- [REST API](https://github.com/yiisoft/app-api)
- [Console](https://github.com/yiisoft/app-console)
This article is based on the `Web App template` as in the past the best `Demo Diary` was not known to me. I will use it for inspiration.
## Documentation
The [official documentation](https://yiisoft.github.io/docs/guide/) is easily available at the home-page, by the "Get Started" button. Click it ...
... and do not miss the interesting link [Cookbook](https://yiisoft.github.io/docs/cookbook/) at the top! It also epxlains lots of things.
## Git
All my code is available in my GitHub repositories:[...]
- https://cycle-orm.dev/docs/annotated-entity/current/en
## Real life insights
### DB vs PHP file
You may soon find out that storing translations and RBAC definitions (user roles and permissions) in DB is a pain in the ... .
Why? Because these are usually only modified by developers and creating migrations because of every small change is tedious.
I recommend storing them in PHP files. In case of RBAC the only thing you want to have in DB is the assignment of users to roles so they can be changed via WEB UI.
## DDD (Domain-Driven Design)
If this is an incomprehensible topic to you, you may be too focused on DB like I was. Let's drop it! Let's forget about DB and ActiveRecord - it is one of the best ways to understand DDD. Without a database, you naturally design your objects around your business rather than database schemas. Then you will easily understand and implement Aggregates, Repositories, Value Objects and other basic stones of DDD.
For example: Value Object is a great tool for auto-validation of small data (email addresses). If you then pass this object as an argument, the recipient can be sure you provided a valid email address. Etc.
So let's try to implement a small in-memory-information-system with just vanilla PHP. DB is an additional and optional layer.
## Vectical slices
It is just a name for a different folder structure. We all know MVC from Yii2 - main folders are Controllers, Models, Views, Migrations and Services.
Note: Services should contain the logic that is usually coded in Controllers, but Controllers should technically only process the request and the Service should do the logic. Controllers are not easily testable (they require the HTTP request), Services are.
So instead of folders:
- /src/Controllers
- /src/Models
- /src/Views
- /src/Migrations
- /src/Services
You bundle all what is related (for example) to User into one folder. Plus: Instead of "Actions" we use name "UseCase":
- /src/User/UseCase/Create/Action.php + view.php
- /src/User/UseCase/Update/Action.php + view.php
- /src/User/Migration/M123.php
- ...
You can say that Vertical slices are MVC rotated by 90° (or flipped inside out)
If anything is shared, you can place it to folder /src/Shared. You can do whatever you want as long as it is a clean and nice solution.
## ===== Yii3 - How to start ======
Yii3 offers more basic applications: Web, Console, API. I will be using the WEB application:
- https://github.com/yiisoft/app
- Other apps are linked on the page[...]