码迷,mamicode.com
首页 > Windows程序 > 详细

Yii2 高级版新建一个 Api 应用

时间:2016-06-29 12:47:58      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

原文地址:http://www.getyii.com/topic/28

先在项目的根目录下复制一份 backend 为 api:

cp backend/ api -r

拷贝 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 里面的控制器等有命名空间的文件也要修改一下。

Yii2 高级版新建一个 Api 应用

标签:

原文地址:http://www.cnblogs.com/LoveJulin/p/5626345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!