This project extends the official Yii3 API application and adds features that I need in every project. If you need basic info about the original application, open their repository and scroll down.
This repository was originally based on my wiki article Yii3 - How to start. In the future it may contain more ideas than the Wiki.
I will be using Docker instead of any other local server technologies so your project will be future-proof. If you don't have Docker, I recommend installing the latest version of Docker Desktop:
Yii3 offers more basic applications: Web, Console, API. I will be using the API application:
- https://github.com/yiisoft/app-api
- Other apps are linked on the page
You may be surprised that docker-compose.yml is missing in the root. Instead, the "make" commands were prepared by the authors. If you run both basic commands as mentioned in the documentation:
- make composer update
- make up
... then the web will be available on your localhost. The port is defined in docker/dev/compose.yml and in docker/.env
- If run via browser, XML is returned
- If run via Postman or Ajax, JSON is returned
As you can see in file docker/dev/compose.yml, I added some containers:
- MariaDB
- Composer - now
composer installshould be called automatically when you callmake up. It is a copy of theappcontainer. - Adminer - the famous single-file DB browser. See their web.
- Yes, adminer.php could be manually places into the public folder, but it could be blocked by your Debugger. Separate container avoids this.
- Adminer will be available on URL http://localhost:9081/?server=db&username=db&db=db.
- Correct port is again in docker/.env, MariaDB login in docker/dev/compose.yml
If you make any changes in your docker setup, just call following commands to restart:
- make down
- make build
- make up
- This will run all containers, but the composer-container will automatically die as soon as
composer installfinishes. - run
docker psto verify
- This will run all containers, but the composer-container will automatically die as soon as
Why did I change the web port? Because later you may run 4 different projects at the same time and all cannot run on port 80.
These composer packages were added using the "make" command:
Now you can call make yii list and you will see migration commands.
When creating a new migration, the best way is:
make yii "migrate:create user --command=table"- Do not forget about the quotes, otherwise "make" will understand it incorrectly
Note: Compared to Yii2, migration names are not nice (example
M251013085231CreateUserTable.php). They are created using method generateClassName() invendor/yiisoft/db-migration/src/Service/MigrationService.php.
The DB connection is defined in file docker/.env and is pushed into containers so it can be reused. I feel the connection should be defined in the dev-related .env file, but it didn't work for me. I will investigate.
Note: If I restart the containers using make down, build + up, then the command "make yii migrate:up" sometimes cannot connect to the DB. Additional restart helped. I will have to test again.
For seeding the DB, I used these packages:
Check src/Console/SeedCommand.php + config/console/commands.php.
Seeding is executed by command make yii seed. List of all available commands is here: make yii list.
In Yii we were always using ActiveRecord and its models, but in Yii3 the package is not ready yet.
The (temporary) solution is to use the existing class Yiisoft\Db\Query\Query for reading -
see src/Entity/BaseRepository.php.
Writing is done in src/Console/SeedCommand.php using Yiisoft\Db\Mysql\Command.
Two endpoints were added:
- src/Api/LoginAction.php
- src/Api/BearerAction.php
The former is used to create and return the access token.
The latter then tests if the token is in the request thanks to the middleware used in config/common/routes.php.
To reach these functionalities, UserToken + its repository were added as well as the migration for storing the tokens.