yii2-restful-api-study3-自定义资源

自定义资源

1.新建控制器,注意继承自Controller,因为没有对应的表资源

<?php
namespace api\controllers;

use yii\rest\Controller;
use api\models\Article;
use yii\db\Query;

//自定义资源

class Top3Controller extends Controller
{
    public function actionIndex()
    {
         $top3 = (new Query())->from('article')->select(['*'])->orderBy('id desc')->limit(3)->all();
        return $top3;
    }
}

?>

2.修改api/config/main.php,rules加入规则

 ['class'=>'yii\rest\UrlRule',
                 'controller'=>'top3',
                 'except'=>['delete','create','update','view'],
                 'pluralize'=>false,//url不带复数
                 ],
发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>