先在项目的根目录下复制一份 backend 为 api:
cp -r backend/ api
拷贝 api 环境
cp -a environments/dev/frontend environments/dev/api
cp -a environments/prod/frontend environments/prod/api
修改 environments/index.php 文件之后的代码(主要是添加了一些 api 相关的代码):
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',
],
],
];
然后再执行初始化命令:
php init
然后记得去 common/config/bootstrap.php 最后一行添加如下代码:
Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');
修改一下配置文件 api/config/main.php
return [
'id' => 'app-api',
// ...
'controllerNamespace' => 'api\controllers',
]
最后 api 里面的控制器等有命名空间的文件也要修改一下。
参考资料
本文由 forecho 创作,采用 知识共享署名 3.0 中国大陆许可协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。
如果这篇文章对您有帮助,不妨微信小额赞助我一下,让我有动力继续写出高质量的教程。