|
| 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