1

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
  • 2
    add parameters to the GET request v1/users?priority=desc&status=asc Commented Apr 5, 2016 at 15:15

1 Answer 1

3

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(),
]);
Sign up to request clarification or add additional context in comments.

4 Comments

Do you maybe know how can I sort by related table column ? I have profile filed that is profile relation with profile table. public function fields() { $fields = parent::fields(); $fields[] = 'profile'; return $fields; } . So I would like to sort by profile.created_at. Do you know how ?
In this case I think you'll need to override $actions['index']['prepareDataProvider'] and add sort to the custom ActiveDataProvider instance via $dataProvider->sort->attributes['profile'] = [..] before returning it like it is done in this example.
I personally use the Search Class. it is where I do filtering and sorting. this is one more example on how to cutomize the searchClass. and this is how I use it in REST
I have asked full new question here: stackoverflow.com/questions/36474282/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.