Yii2快速构建RESTful Web ActiveController

1. 先用Gii 生成一个Model

2. 新建一个Controller 继承 ActiveController

<?php
namespace app\controllers;


use yii\rest\ActiveController;
use app\models\Article;

class ArticleController extends ActiveController
{
    public $modelClass = 'app\models\Article';
}
 

3. URL 美化

'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule','controller' => 'article'],
            ],
        ],

4. json输出

'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => '123456',
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
             ]
        ],
 

Yii2 failed to live composer


 

猜你喜欢

转载自blog.csdn.net/fox64194167/article/details/80286700