Skip to content

Commit 9d77b1f

Browse files
committed
WIP api generator
1 parent 50ad603 commit 9d77b1f

File tree

12 files changed

+209
-187
lines changed

12 files changed

+209
-187
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
/*/*.pid
44
/*/*.log
55
session_mm_*.sem
6+
7+
/env.php

README.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,59 @@
44
55
Yii Framework Application Template for quickly building API-first applications.
66

7+
## Overview
8+
9+
This application template consists of 3 application tiers:
10+
11+
- `api`, contains the Yii application for the REST API.
12+
- `console`, contains the Yii application for console commands, cronjobs or queues.
13+
- `backend`, contains the Yii application for a CRUD backend on the API data.
14+
15+
716
## Setup
817

918
git clone https://github.com/cebe/yii2-app-api my-api
1019
cd my-api
20+
cp env.php.dist env.php
1121
composer install
1222

13-
## Overview
23+
## Generate Code
1424

15-
This application consists of 3 parts:
25+
### Console
1626

17-
- `api`, contains the Yii application for the REST API.
18-
- `console`, contains the Yii application for console commands, cronjobs or queues.
19-
- `backend`, contains the Yii application for a CRUD backend on the API data.
27+
Run `./yii gii/api` to generate your API code. The `--openApiPath` parameter specifies the path to your OpenAPI
28+
spec file. The following example will generate API code for the [OpenAPI petstore example](https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/examples/v3.0/petstore-expanded.yaml).
29+
30+
./yii gii/api --openApiPath=https://raw.githubusercontent.com/OAI/OpenAPI-Specification/3.0.2/examples/v3.0/petstore-expanded.yaml
31+
32+
Run `./yii gii/api --help` for a list of configuration options. You may also adjust the configuration in `config/gii-generators.php`.
33+
34+
Then set up the database:
35+
36+
./yii migrate/up
37+
./yii faker
38+
39+
### Web
40+
41+
To use the web generator, start the backend server:
42+
43+
cd backend
44+
make start
45+
46+
open `http://localhost:8338/gii` and select the `REST API Generator`.
47+
48+
![Gii - REST API Generator](docs/img/gii-generator.png)
49+
50+
Enter the path or URL to the "OpenAPI 3 Spec file", e.g. `https://raw.githubusercontent.com/OAI/OpenAPI-Specification/3.0.2/examples/v3.0/petstore-expanded.yaml`.
51+
52+
Click "Preview":
53+
54+
![Gii - REST API Generator - Generated files](docs/img/gii-generator-files.png)
55+
56+
Click "Generate" to generate API files.
57+
58+
Then set up the database by running the following commands on the command line:
2059

60+
./yii migrate/up
61+
./yii faker
2162

api/config/url-rules.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
<?php
22

3-
// TODO configure URL rules with autogenerated spec rules
4-
5-
return [
6-
'' => 'home/index',
7-
[
8-
'class' => yii\rest\UrlRule::class,
9-
'controller' => ['home'],
10-
'pluralize' => false,
11-
'extraPatterns' => [
12-
'GET index' => 'index'
13-
]
14-
]
3+
$customRules = [
4+
// add your custom URL rules here
155
];
6+
7+
if (file_exists(__DIR__ . '/url-rules.rest.php')) {
8+
return array_merge(require __DIR__ . '/url-rules.rest.php', $customRules);
9+
}
10+
return $customRules;

backend/config/app-dev.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
$config['bootstrap'][] = 'gii';
1919
$config['modules']['gii'] = [
2020
'class' => yii\gii\Module::class,
21-
'generators' => [
22-
// add ApiGenerator to Gii module
23-
'api' => \cebe\yii2openapi\generator\ApiGenerator::class,
24-
],
21+
// add ApiGenerator to Gii module
22+
'generators' => require __DIR__ . '/../../config/gii-generators.php',
2523
];
2624

2725
return $config;

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"name": "cebe/yii2-app-api",
23
"require": {
34
"php": ">=7.1.0",
45
"yiisoft/yii2": "~2.0.16",

0 commit comments

Comments
 (0)