yii rest

Environment to build

phpstorm + phpstudy +xdebug

Download and install

REST

A copy of the new catalog of background, set config/main.phpthe id and controller namespaces

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

Setting url landscaping rules

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

In main-local.phpsetting json parser

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

Nginx apache configuration file or modify configuration files

Controller

inherit ActiveController

use yii\data\ActiveDataProvider;
use yii\rest\ActiveController;

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

Field acquisition

Additional fields url parameters

Rewrite fields method

By custom field value in the anonymous function fields method

By rewriting extrafields way to ignore certain fields

Paging

use yii\data\ActiveDataProvider;
use yii\rest\ActiveController;

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

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index']);
        return $actions;
    }

    function actionIndex(){
        $modelClass = $this->modelClass;
        return new ActiveDataProvider([
            'query'=>$modelClass::find()->asArray(),
            'pagination' => ['pageSize'=>5],
        ]);
    }
}

access http://apitpl.com/articles?page=2

search for

In the method of adding the controller

    function actionSearch()
    {
        return $this->modelClass::find()->where(['like', 'title', $_POST['keyword']])->all();
    }

Modify the configuration file

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

Access path http://apitpl.com/articles/search, post way
body:keyword=''

Custom Resources

Inheritance Controller, not ActiveController
top10

use common\models\Article;
use yii\db\Query;
use yii\rest\ActiveController;
use yii\rest\Controller;

class TopController extends Controller
{

    public function actionIndex()
    {
        $all = Article::find()
            ->select([
                '序号' => 'article.id',
                '标题' => 'article.title',
                '状态' => 'article.status',
                '作者' => 'user.username'])
            ->leftjoin('user', 'user.id=article.created_by')
            ->orderBy('序号 desc')
            ->limit(5)
            ->asArray();



//        echo $all->createCommand()->getRawSql() . "\n";


//        $all = (new Query())
//            ->select(['article.id', 'article.title', 'user.username'])
//            ->from(['article', 'user'])
//            ->where('article.created_by=user.id')
//            ->orderBy('article.id desc')
//            ->limit(5);
//
//        echo $all->createCommand()->getRawSql() . "\n";


        return $all->all();
    }

}

If the function does not return data, and the data can sql statement, call asArray()the method

Guess you like

Origin www.cnblogs.com/gaoyongjian/p/12132098.html