first copy the backend to API:CP in the root directory of the project backend/API-r Copy API Environment CP-A environments/dev/frontend environments/dev/APICP-A environments/prod/frontend environments/prod/API Modification Environments/index.php file after the code (mainly added some API-related code): Copy the Codereturn [ ' 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 ', ], ],]; Copy the code and then execute the initialization command: PHP init then remember to go to common/config/bootstrap.Add the following code to the last line of PHP: Yii:: Setalias (' API ',dirname(dirname(__dir__)) . '/api '); Modify the configuration file API/config/main.PHPreturn [ ' id ' = ' app-api ',// ... ' Controllernamespace ' = ' api\controllers ',] Finally, the API inside the controller and other namespaces have to modify the file.
New Api App for YII2 Premium Edition