Comparing
#63 with
#82
Content
## Intro
In Yii3 it is not as easy to start as it was with Yii2. You have to install and configure basic things on your own. Yii3 uses the modern approach based on independent packages and dependency injection, but it makes it harder for newcomers. I am here to show all how I did it.
> If you want to know how Yii2 works, check my other wiki and demo application:
>
> [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.
## Git
All my code is available in my GitHub repositories:[...]
I will be using it as a boiler-plate for my future projects so it should be always up-to-date and working.
My app is based on the official demo
- [Yii3 web application](https://github.com/yiisoft/app)
## Docker
Instead of installing local WAMP- or XAMPP-server I will be using Docker. Do not forget about a modern IDE like PhpStorm, which comes bundled with all you will ever need.[...]
## ===== Yii3 - How to start
======
Yii3 offers more basic applications: Web, Console, API. I will be using the WEB application:[...]
In this text I am mainly working with API endpoints, but UI will be presented later as well.
## .env files
There are quite a lot of `.env` files which was confusing. But in short:
- File `/.env.example` should be copied and renamed to `/.env` only if you are NOT using Docker.
- Then there are the `.env` files in the docker folder. To understand, just study file `./Makefile`. It is important to know that this file is here to call docker commands for you. Do not call docker manually. This is the important philosophy.
- In the `./Makefile` you will see command `include docker/.env` and below more `compose.yml` are loaded using the `-f` argument
- So `docker/.env` is only visible in files `compose.yml`
- If you need to inject any environment variable into your PHP application, you can place it into the `.env` files that are used inside (for example) `docker/dev/compose.yml`.
## Running the demo application
You may be surprised that docker-compose.yml is missing in the root. Instead the "make up", "make stop" and "make down" commands are prepared.[...]
```
## GridView + CRUD for Users
Grid view is in package `yiisoft/yii-dataview`. The source code is extremely verbose and complex to me
(compared to how primitive function it should provide), so I am just linking my GitHub demo:
- [
IndexAction](https://github.com/rackycz/yii3web/blob/master/src/User/Action/IndexAction.php)
- [method findAll()](https://github.com/rackycz/yii3web/blob/master/src/Entity/QueryBuilder/UserQueryBuilderRepository.php)
- [QueryDataReader](https://github.com/rackycz/yii3web/blob/master/src/Entity/QueryBuilder/QueryDataReader.php)
- [View](https://github.com/rackycz/yii3web/blob/master/src/User/View/index.php)
> In the UpdateAction I am presenting the ActiveRecord
Below you can also see how I created the UpdateAction + form to enable editing users.
- [UpdateAction](https://github.com/rackycz/yii3web/blob/master/src/User/Action/UpdateAction.php)
- [View](https://github.com/rackycz/yii3web/blob/master/src/User/View/update.php)
- [Form + Validation](https://github.com/rackycz/yii3web/blob/master/src/User/Form/UserUpdateForm.php)
> In Yii3, validation rules are not part of the model - special form-class must be created. In Yii2 you didn't need the form-class for model-driven (db-driven) forms.
## ActiveRecord
Example of usage is in the following files. ActiveRecord combines 2 classes: Entity + Repository = data and DB manipulation.
- [User](https://github.com/rackycz/yii3web/blob/master/src/Entity/ActiveRecord/User.php)
- [UpdateAction](https://github.com/rackycz/yii3web/blob/master/src/User/Action/ActiveRecord/UpdateAction.php)
## JS client - Installable Vuejs3 PWA
If you create a REST API you may be interested in a JS frontend that will communicate with it using Ajax. Below you can peek into my very simple VueJS3 attempt. It is an installable PWA application that works in offline mode (=1 data transfer per day, not on every mouse click) and is meant for situations when customer does not have wifi everywhere. See my [Gitlab](https://gitlab.com/radin.cerny/vuejs3-pwa-demo-plus).