Filter by other fields on Yii2 RESTful API

jfl*_*dro 3 php rest yii yii2

First sorry for bad english!

I'm testing a RESTful API in YII2 and it's so easy to create, following the official guide. But by default (as far as I know) i can only pass the id as param to get a specific record.

For example, supposing that i have the following table called person:
id, name, age, gender, email, phone

In this case, i can only filter by id, like this: http://myserver/api/persons/1

我需要知道如何按其他字段过滤,例如年龄性别

我的控制器:

class PersonController extends ActiveController
{
    public $modelClass = 'app\models\Person';
}
Run Code Online (Sandbox Code Playgroud)

谢谢你!

Sal*_*ani 5

ActiveController类中实现的默认IndexAction返回的ActiveDataProvider实例不支持按属性过滤:


app/vendor/yiisoft/yii2/rest/IndexAction.php :

(...)

protected function prepareDataProvider()
{
    (...)

    return new ActiveDataProvider([
        'query' => $modelClass::find(),
    ]);
}
Run Code Online (Sandbox Code Playgroud)

您将需要使用自定义代码覆盖它。这是一个示例,它被gii的内置搜索模型类返回的ActiveDataProvider实例覆盖:检查此链接