Revision #2 has been created by
rackycz on Oct 8, 2025, 12:32:04 PM with the memo:
(draft)
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 them ...
Note:
- Instead of installing local WAMP
- or XAMPP
-server I will be using Docker.
- Do not forget about a modern IDE like PhpStorm, which comes budled with all you will ever need.[...]
- git clone https://github.com/yiisoft/app-api.git yii3api
.. and follow the **docker** instructions in the documentation.
If you don't have Docker, I recommend installing the **latest** version of Docker Desktop:[...]
# 3) Adding DB into your project
Your dockerproject now does not contain any DB. Let's add
mMariaDB and Adminer (DB browser) into file docker/dev/compose.yml:
In my case the resulting file looks like this:[...]
- DEV_ADMINER_PORT=9081
Change the files andThen run following commands:
- make down
- make build
- make up
ThenNow you should see a DB browser on URL http://localhost:9081/?server=db&username=db&db=db
Login, server and pwd is defined in the snippet above.[...]
Other solutions:
> - Locally:
> If you have Composer running locally, you can call these commands directly on your computer. (I do not recommend)
> - Docker:
> You can SSH into your docker container and call it there as Composer is installed inside.
> Find the name of the PHP container by typing "docker ps" and call:
> docker exec -it {containerName} /bin/bash
> Now you are in the console of your php server and you can run composer.
> - PhpStorm:
> If you are using PhpStorm, find the small icon "Services" in the left lower corner (looks ca like a cog wheel), find item "Docker-compose: app-api", inside click the "app" service, then "yii3api_php" container and then hit the button "terminal" on the righthand side.
## b) Setting up composer packages
Follow their documentations. Quick links:[...]
- https://github.com/yiisoft/db-migration/blob/master/docs/guide/en/README.md (I recommend "Yii Console" installation)
Note:> The documentations want you to create 2 files:
- config/common/di/db-mysql.php
- config/common/db.php
... you actually need only one. I recommend
**db-mysql.php
**
> Note: If you want to create a file using commandline, you can use command "touch". For example "touch config/common/di/db-mysql.php"
n
> Note: In the documentation the PHP snippets do not contain tag and declaration. Prepend it:
```php
<?php
declare(strict_types=1);
```
## c) Create folder for migrations
- src/Migration
When this is done, call "composer du" or "make composer du" and then try "make yii list". You should see the migration commands.
# 5) Creating a migration
Run
the command to create a migration:
- make yii migrate:create user
Open the file and paste following content to the up() method:
```php
$b->createTable('user', [
'id' => $b->primaryKey(),
'name' => $b->string()->notNull(),[...]
'deleted_at' => $b->dateTime(),
]);
```
The down() method should contain this:
```php
$b->dropTable('user');
```
# 6) Running the migrations
Try to run "make yii migrate:up" and you will see error "could not find driver", because file "docker/Dockerfile" does not install the "pdo_mysql" extention. Add it to the where "install-php-extensions" is called.[...]
Now you will see error "Connection refused"
It means you have ot
o update dns, user and password in file "config/common/params.php" based on what is written in "docker/dev/compose.yml".
If you run "make yii migrate:up" it should work now and your DB should contain the first table. Check it via adminer:
http://localhost:9081/?server=db&username=db&db=db