platx/yii2-rest

用于API模块的Rest类。

安装次数: 2,227

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 3

分支: 2

公开问题: 3

类型:yii2-extension

dev-master 2016-07-08 09:05 UTC

This package is not auto-updated.

Last update: 2020-10-02 21:10:02 UTC


README

用于API模块的Rest类。仅限个人使用!

安装

推荐通过 composer 安装此扩展。

运行以下命令之一

php composer.phar require --prefer-dist platx/yii2-rest "*"

"platx/yii2-rest": "*"

将以下内容添加到您的 composer.json 文件的 require 部分。

用法

在您的模型中,您可以设置用于API的字段和extraFields

class MyModel extends \yii\db\ActiveRecord
{
    public function fields()
    {
        $items = parent::fields();
        unset($items['id']);
        return $items;
    }
    public function extraFields()
    {
        return [
            'custom_field' => 'customField'
        ];
    }
    public function getCustomField()
    {
        return 'Custom value';
    }
}

为您的ActiveRecord模型创建搜索表单类

class MySearchForm extends \platx\rest\SearchForm 
{
    public $modelClass = '\common\models\MyModel';
    public function rules()
    {
        return \yii\helpers\ArrayHelper::merge(parent::rules(), [
            // HERE PUT YOUR VALIDATION RULES
        ]);
    }
    public function attributeLabels()
    {
        return \yii\helpers\ArrayHelper::merge(parent::attributeLabels(), [
            // HERE PUT YOUR LABELS FOR ATTRIBUTES
        ]);
    }
    public function filterAttributes()
    {
        parent::filterAttributes();
        // HERE type your filter rules, like:
        // $this->query->andFilterWhere(['like', 'attribute', $this->attribute]);
        // ...
    }
}

从 \platx\rest\Controller 扩展您的控制器,如下所示

class MyController extends \platx\rest\Controller 
{
    public function actions()
    {
        public $modelClass = '\common\models\MyModel';
        public $searchFormClass = '\common\forms\MySearchForm';
        return [
            'index' => [
                'class' => '\platx\rest\actions\IndexAction',
                'searchFormClass' => $this->searchFormClass
            ],
            'view' => [
                'class' => '\platx\rest\actions\ViewAction',
                'modelClass' => $this->modelClass
            ],
            'create' => [
                'class' => '\platx\rest\actions\CreateAction',
                'modelClass' => $this->modelClass
            ],
            'validate' => [
                'class' => '\platx\rest\actions\ValidateAction',
                'modelClass' => $this->modelClass
            ],
            'update' => [
                'class' => '\platx\rest\actions\UpdateAction',
                'modelClass' => $this->modelClass
            ],
            'delete' => [
                'class' => '\platx\rest\actions\DeleteAction',
                'modelClass' => $this->modelClass
            ],
        ];
    }
}

在API Index操作中,您可以使用查询构建器: /api/my?where[id][in]=1,2,3/api/my?where={"id":{"in":[1,2,3]}}

可用运算符列表

运算符 描述
eq 一个 参数等于值
neq 一个 参数不等于值
like 一个 值是参数的子串
nlike 一个 值不是参数的子串
in 多个 属性等于多个值之一
nin 多个 属性不等于多个值之一
gt 一个 属性大于值
gteq 一个 属性大于等于值
lt 一个 属性小于值
lteq 一个 属性小于等于值

在您的配置中,如果想要像API模块一样使用API,请将此规则添加到UrlManager规则中

'rules'=>[
    ...
    'GET <module:api>/<controller:[\w-]+>' => '<module>/<controller>/index',
    'POST <module:api>/<controller:[\w-]+>' => '<module>/<controller>/create',
    'GET <module:api>/<controller:[\w-]+>/<id:\d+>' => '<module>/<controller>/view',
    'PUT <module:api>/<controller:[\w-]+>/<id:\d+>' => '<module>/<controller>/update',
    'DELETE <module:api>/<controller:[\w-]+>/<id:\d+>' => '<module>/<controller>/delete',
    'POST <module:api>/<controller:[\w-]+>/<action:validate>' => '<module>/<controller>/<action>',
    '<module:api>/<controller:[\w-]+>/<action:[\w-]+>/<id:\d+>' => '<module>/<controller>/<action>',
    '<module:api>/<controller:[\w-]+>/<action:[\w-]+>' => '<module>/<controller>/<action>',
    ...
]

现在您可以使用API了,例如

GET http://your-site.com/api/my - List of records
GET http://your-site.com/api/my?offset=2&limit=5 - Select records from second to seventh
GET http://your-site.com/api/my?where={"id": {"gteq": 2, "lt": 5}} - Select records with id greater than or equal 2 and less than 5
GET http://your-site.com/api/my?sort=-created_at - List of records with sorting descending by created_at attribute
GET http://your-site.com/api/my?sort=created_at - List of records with sorting ascending by created_at attribute
GET http://your-site.com/api/my?fields=id - List of records with selecting just id attribute
GET http://your-site.com/api/my?fields=id,name - List of records with selecting just id and name attribute
GET http://your-site.com/api/my?expand=custom_field - List of records with selecting fields value from extraFields array in our model
GET http://your-site.com/api/my/1 - Get one record by id attribute
POST http://your-site.com/api/my - Create new record
POST http://your-site.com/api/my/validate?fields=name,title - Validate specified fields.
PUT http://your-site.com/api/my/1 - Update record attributes
DELETE http://your-site.com/api/my/1 - Delete record