First copy the backend to the API at the root of the project:
CP Backend/api-r
Copy API Environment
Cp-a environments/dev/frontend environments/dev/-a environments/prod/frontend Environments/prod/api
Modify the code after the environments/index.php file (mainly adding some API-related code):
return [ ' Development ' = [ ' Path ' = ' dev ', ' setwritable ' = [ ' Backend/runtime ', ' backend/web/assets ', ' frontend/runtime ', ' frontend/web/assets ', ' Api/runtime ', ' api/web/assets ', ], ' setexecutable ' = [ ' Yii ', ], ' setcookievalidationkey ' = [ ' backend/config/main-local.php ', ' frontend/config/main-local.php ', ' api/config/main-local.php ', ], ], ' Production ' = [ ' Path ' = ' prod ', ' setwritable ' = [ ' Backend/runtime ', ' backend/web/assets ', ' frontend/runtime ', ' frontend/web/assets ', ' Api/runtime ', ' api/web/assets ', ], ' setexecutable ' = [ ' Yii ', ], ' setcookievalidationkey ' = [ ' backend/config/main-local.php ', ' frontend/config/main-local.php ', ' api/config/main-local.php ', ], ],];
Then execute the initialization command:
PHP Init
Then remember to add the following code to the last line of common/config/bootstrap.php:
dirname (dirname(__dir__)). '/api ');
Modify the configuration file api/config/main.php
return [ ' id ' = ' app-api ', // ' controllernamespace ' = ' api\controllers ', ]
Finally, the API inside the controller and other namespaces have to modify the file.
Resources
- On the original YII2 framework, create a new API application
- First Lecture: Basic configuration
Source: Http://www.getyii.com/topic/28
New Api App for YII2 Premium Edition