Skip to content

Commit a59fa84

Browse files
committed
Improved docs
1 parent 227a153 commit a59fa84

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ You can now continue with [generating code](#generate-code).
8484

8585
## Generate Code <span id="generate-code"></span>
8686

87+
At this point you can start generating your API code to play with it by following the instructions below.
88+
If you'd like to start a real project instead of just playing around you can find more detailed
89+
documentation in [docs/README.md](docs/README.md) specifically the ["Getting Started"-Section](docs/gettings-started.md).
90+
8791
### Console
8892

8993
> Tip: If you use Docker, run `make cli` before running any of these commands.

docs/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ Documentation
33

44
This directory contains documentation and tutorials that show how to work with this application template.
55

6+
- [Getting started with a new API](getting-started.md)
67
- [How to generate a Backend with Bootstrap 4](backend-bootstrap4.md)
8+
9+

docs/getting-started.md

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
Getting started with a new API
2+
==============================
3+
4+
After setting up the project as described in [README.md under "Setup"](../README.md#setup-) you
5+
can follow this guide to get started with your API.
6+
7+
## Create the OpenAPI description file
8+
9+
If you don't have an OpenAPI description already you should first create one.
10+
Working with this project template you are following the
11+
[Design-First-Approach](https://apisyouwonthate.com/blog/api-design-first-vs-code-first),
12+
so before you write any code, you are creating an API description.
13+
14+
You are free to choose the path and file name where you want to put your OpenAPI description file.
15+
In this guide we are placing all OpenAPI files in the `openapi` directory.
16+
Our API description is in `openapi/schema.yaml`.
17+
If you choose another file location, you need to take that into account when configuring the generator
18+
in the next step.
19+
20+
If you don't know how to create OpenAPI descriptions you can check out the following resources:
21+
22+
- <https://oai.github.io/Documentation/start-here.html>
23+
- ... (if you happen to know better resources to look at, please [let us know](https://github.com/cebe/yii2-app-api/issues/new)
24+
25+
Also check out the [OpenAPI Specification](https://spec.openapis.org/oas/v3.1.0) itself.
26+
27+
28+
## Configure the Code Generator
29+
30+
The Code Generator (based on [Gii](https://www.yiiframework.com/doc/guide/2.0/en/start-gii), the Yii Framework code generator) generates the Database, Model classes, API Controllers and routing rules based on the OpenAPI description file.
31+
32+
The Code Generator configuration is located in `config/gii-generators.php`.
33+
34+
The first thing to configure for a new project is the `openApiPath` setting, which tells the generator where to look for the OpenAPI description file:
35+
36+
```php
37+
return [
38+
'api' => [
39+
// ... keep existing settings, add the OpenAPI file:
40+
41+
'openApiPath' => '@root/openapi/schema.yaml',
42+
],
43+
];
44+
```
45+
46+
You may run `./yii gii/api --help` for a list of configuration options.
47+
All options may be specified as command line options or in the configuration file `config/gii-generators.php`.
48+
49+
50+
## Generate Code
51+
52+
At this point you should have started the application server, we assume you are using the Docker environment here, so all commands are run after running `make start-docker` and `make cli`.
53+
54+
Run the code generator:
55+
56+
./yii gii/api
57+
58+
This command checks the OpenAPI description and compares it to the existing database. On the first run it will generate migrations to create all mising tables. If you want to run it again you always need to apply these migrations to make sure the generator compares to the current state of the database. Otherwise it would generate the same migrations again.
59+
60+
Apply migrations with:
61+
62+
./yii migrate/up
63+
64+
The generator also creates Fake data generators. These can be run using the following command:
65+
66+
./yii faker
67+
68+
The fake data is based on guesses, in many cases you'd want to adjust the faker to produce more accurate data. You can do that by adding the `x-faker` property to your OpenAPI schema. This is an OpenAPI extension provided by the [yii2-openapi](https://github.com/cebe/yii2-openapi) package.
69+
70+
You can find more information on OpenAPI extensions the [README](https://github.com/cebe/yii2-openapi#openapi-extensions) of that package.
71+
72+
### Making changes to the specification
73+
74+
To add more Models/Tables or Paths you first adjust your OpenAPI description and then run the generator again. Make sure all migrations have been applied before that.
75+
76+
./yii gii/api
77+
78+
This will update the existing Models and Controllers and also create migrations for the changes that are made to the database.
79+
80+
It is important to check the migrations manually, for example if you rename columns it will create drop column and create column instaead of rename which may result in data loss.
81+
82+
For each Model and Controller you have a base class that is controlled by the Generator, changes to the base file will be overwritten every time you run the generator. If you want to make changes to the class you can do that in the subclasses, which are only created on the first run and will not be touched on subsequent generator runs.
83+
84+
> Note: There are currently some bugs in the Database part of the generator which results
85+
> in wrong migrations being creatated. This mainly affects MySQL and MariaDB,
86+
> PostgreSQL should be working fine.
87+
>
88+
> If you run into these you need to adjust the generated migrations manually. Some migrations should be deleted if they contain only wrong changes.
89+
> See <https://github.com/cebe/yii2-openapi/issues>, specifically <https://github.com/cebe/yii2-openapi/issues/100> and <https://github.com/cebe/yii2-openapi/issues/111>
90+
91+
92+
93+
94+

0 commit comments

Comments
 (0)