How to send Yii2 RESTful GET request and specify sorting as params

耗尽温柔 提交于 2019-12-24 15:37:49

问题


I want to send GET request to v1/users endpoint, where as params I want to specify that I want to do ordering by priority DESC and status ASC.

As you can see I want to send params that will map to SQL WHERE parts:

SELECT * FROM table WHERE something ORDER BY priority DESC, status ASC

How I am supposed to specify as params in my HTTP request that I want this sorting ? I think that in order to do this I need to send JSON data in POST request. But that is a problem, because I want GET request not POST. post/users means create user, and I want to get users.

I guess that I would have to send JSON object like this

"sort":[ {"priority":"DESC", "status":"ASC"} ]

First, is it possible to send params like this when you send GET request ?
Second, how would you send these params using cUrl in PHP ?


回答1:


with the built-in RESTfull API you can use comma for multi attributes sorting and '-' sign for DESC:

GET v1/users?sort=-priority,status

If using a custom action instead of the built-in onces. be sure to always return a data provider instance so the serializer can generate related pagination and the above params get suppoted:

// instead of: return $modelClass::find()->all();
return new ActiveDataProvider([
    'query' => $modelClass::find(),
]);


来源:https://stackoverflow.com/questions/36430046/how-to-send-yii2-restful-get-request-and-specify-sorting-as-params

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!