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',
             ]
        ],

http://www.waitingfy.com/archives/3466

3466

Leave a Reply

Name and Email Address are required fields.
Your email will not be published or shared with third parties.