@@ -5,6 +5,8 @@ API Platform Core provides an easy way to override the default order of items in
5
5
By default, items in the collection are ordered in ascending (ASC) order by their resource identifier(s). If you want to
6
6
customize this order, you must add an ` order ` attribute on your ApiResource annotation:
7
7
8
+ [ codeSelector]
9
+
8
10
``` php
9
11
<?php
10
12
// api/src/Entity/Book.php
@@ -28,9 +30,21 @@ class Book
28
30
}
29
31
```
30
32
33
+ ``` yaml
34
+ # config/api/resources/Book.yaml
35
+ App\Entity\Book :
36
+ attributes :
37
+ order :
38
+ foo : ASC
39
+ ` ` `
40
+
41
+ [/codeSelector]
42
+
31
43
This ` order` attribute is used as an array: the key defines the order field, the values defines the direction.
32
44
If you only specify the key, `ASC` direction will be used as default. For example, to order by `foo` & `bar` :
33
45
46
+ [codeSelector]
47
+
34
48
` ` ` php
35
49
<?php
36
50
// api/src/Entity/Book.php
@@ -59,8 +73,19 @@ class Book
59
73
}
60
74
` ` `
61
75
76
+ ` ` ` yaml
77
+ # config/api/resources/Book.yaml
78
+ App\E ntity\B ook:
79
+ attributes:
80
+ order: ['foo', 'bar']
81
+ ` ` `
82
+
83
+ [/codeSelector]
84
+
62
85
It's also possible to configure the default order on an association property :
63
86
87
+ [codeSelector]
88
+
64
89
` ` ` php
65
90
<?php
66
91
// api/src/Entity/Book.php
@@ -84,8 +109,19 @@ class Book
84
109
}
85
110
` ` `
86
111
112
+ ` ` ` yaml
113
+ # config/api/resources/Book.yaml
114
+ App\E ntity\B ook:
115
+ attributes:
116
+ order: ['author.username']
117
+ ` ` `
118
+
119
+ [/codeSelector]
120
+
87
121
Another possibility is to apply the default order for a specific collection operation, which will override the global default order configuration.
88
122
123
+ [codeSelector]
124
+
89
125
` ` ` php
90
126
/**
91
127
* collectionOperations={
@@ -106,3 +142,21 @@ class Book
106
142
// ...
107
143
}
108
144
` ` `
145
+
146
+ ` ` ` yaml
147
+ # config/api/resources/Book.yaml
148
+ App\E ntity\B ook:
149
+ get: ~
150
+ get_desc_custom:
151
+ method: get
152
+ path: custom_collection_desc_foos
153
+ order:
154
+ name: DESC
155
+ get_asc_custom:
156
+ method: get
157
+ path: custom_collection_asc_foos
158
+ order:
159
+ name: ASC
160
+ ` ` `
161
+
162
+ [/codeSelector]
0 commit comments