准备工作

0.pre-install xampp,开启服务器和数据库,php版本号5.5+,将yourpath/xampp/bin写入环境变量

    //mac上配置环境变量
    vi ~/.bash_profile 
    export PATH=/Applications/xampp/xamppfiles/bin/:$PATH

windows 我的电脑 – 属性 – 更改设置 – 高级 – 环境变量 – 系统变量 – Path
添加 e:\xampp\php;e:\xampp\php\ext;
windows下面有时候会提示:api-ms-win-crt-runtime-l1-1-0.dll 丢失,这就需要去下载一个补丁
https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=49077
有的系统提示补丁装不上,可能需要安装win7 sp1
1.yiiframework上下载advanced安装包,解压 tar zxvf yii-advanced-app-2.0.13.tgz
2.进入advanced 目录: 执行 php init
3.配置database , common/config/main-local.php,修改dbname为advanced_yii2
4.新建advanced_yii2数据库,执行 php yii migrate 自动会在新建的数据库里添加两张表,
5.访问http://www.xxxx.com/…frontend/web/ 注册一个用户,确保用户signup成功
到这里没出错的话,yii2框架安装完毕!
ps:web/index.php里面可以修改是否开启debug模式
ps:确保apache开启了rewrite_mod(httpd.conf里面修改), 入口文件夹web下建立好.htaccess文件(windows下cmd命令: ren 1.txt .htaccess)如下:

Options +FollowSymLinks
IndexIgnore */*

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

RewriteRule \.svn\/  /404.html
RewriteRule \.git\/  /404.html

接下来开始restful-api之旅:

1. 修改api/config/main.php

cd advanced
cp -r backend api //把backend拷贝一份成api
cd api/config
vim main.php //修改三个地方

第一个地方:改id为app-api,改控制器命名空间的路径为api\controllers

return [
    'id' => 'app-api',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'api\controllers',
]

第二个地方:修改路由规则,按restful风格来,以后加个控制器,这个地方都要改

`'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        ['class' => 'yii\rest\UrlRule', 
        // 'controller' => ['user','article'], //多个控制器支持restful的写法
        'controller' => 'user', //注意这个地方有个逗号,对应版本yii-advanced-app-2.0.13
        ] //注意这个地方没有标点符号,我找这个错误找了2个多小时,泪奔~~
    ],
]`

第三个地方:
common/config/bootstrap.php下面添加
Yii::setAlias('@api', dirname(dirname(__DIR__)) . '/api');

2.在api/controllers/下面新加个控制器文件UserController.php

<?php
namespace api\controllers;
use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'common\models\User';//就定义了个模型指针,然后框架就把restful相关功能全部实现一遍了,是不是很爽
}

3.修改common\config\bootstrap.php

添加一个新的别名:
Yii::setAlias(‘应用名’, dirname(dirname(DIR)) . ‘/目录名’);
比如:Yii::setAlias(‘mobile’, dirname(dirname(DIR)) . ‘/mobile’);
ps:如果漏掉这个,页面会报错:’Unable to resolve the request “site/error”.’

4. postman 测试

get访问http://www.xxxx.com/…api/web/users
可以看到之前注册的用户信息,user表比较特殊,post/put暂时不起作用,可以自建test数据表及对应模型进行测试
建议弄个虚拟目录,/etc/hosts里面绑个开发用的域名,指向api/web,调试起来更方便

5.上面4步弄好,post只支持x-www-form-urlencoded格式数据,如果要支持post raw json数据,则需要修改api/config/main-local.php

$config = [
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'WWiWFkBqTXFYeC6e2gpe3ZRi-LxjkxnI',
            'parsers' =>[
                'application/json'=>'yii\web\JsonParser',
                //'text/json' => 'yii\web\JsonParser',
            ],//注意这个逗号不要忘记加了,啥也不说了,泪奔~
        ],
    ],
];

ps: 对应的数据model里面需要定义如下函数(即数据库字段对应的验证规则),否则post和put不起作用:

public function rules()
    {
        return [
            [['name', 'pwd'], 'string', 'max' => 11],
        ];
    }