问题
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