Skip to content

Commit 085869b

Browse files
vincentchalamonmeyerbaptiste
authored andcommitted
api-platform/core#1246: configure default order on ApiResource annotation (#235)
1 parent 9513635 commit 085869b

File tree

3 files changed

+77
-11
lines changed

3 files changed

+77
-11
lines changed

core/default-order.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Overriding Default Order
2+
3+
API Platform Core provides an easy way to override the default order of items in your collection.
4+
5+
By default, items in the collection are ordered in ascending (ASC) order by their resource identifier(s). If you want to
6+
customize this order, you must add an `order` attribute on your ApiResource annotation:
7+
8+
```php
9+
<?php
10+
11+
// src/AppBundle/Entity/Book.php
12+
13+
namespace AppBundle\Entity;
14+
15+
use ApiPlatform\Core\Annotation\ApiResource;
16+
17+
/**
18+
* @ApiResource(attributes={"order"={"foo": "ASC"}})
19+
*/
20+
class Book
21+
{
22+
// ...
23+
24+
/**
25+
* ...
26+
*/
27+
public $foo;
28+
}
29+
```
30+
31+
This `order` attribute is used as an array: the key defines the order field, the values defines the direction.
32+
If you only specify the key, `ASC` direction will be used as default. For example, to order by `foo` & `bar`:
33+
34+
```php
35+
<?php
36+
37+
// src/AppBundle/Entity/Book.php
38+
39+
namespace AppBundle\Entity;
40+
41+
use ApiPlatform\Core\Annotation\ApiResource;
42+
43+
/**
44+
* @ApiResource(attributes={"order"={"foo", "bar"}})
45+
*/
46+
class Book
47+
{
48+
// ...
49+
50+
/**
51+
* ...
52+
*/
53+
public $foo;
54+
55+
/**
56+
* ...
57+
*/
58+
public $bar;
59+
}
60+
```
61+
62+
Previous chapter: [Operations](operations.md)
63+
64+
Next chapter: [Filters](filters.md)

core/operations.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,5 @@ book_special:
525525
526526
Previous chapter: [Configuration](configuration.md)
527527
528-
Next chapter: [Filters](filters.md)
528+
Next chapter: [Overriding Default Order](default-order.md)
529+

index.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
2. [Configuring Operations](core/operations.md#configuring-operations)
1919
3. [Subresources](core/operations.md#subresources)
2020
4. [Creating Custom Operations and Controllers](core/operations.md#creating-custom-operations-and-controllers)
21-
5. [Filters](core/filters.md)
21+
5. [Overriding Default Order](core/default-order.md)
22+
6. [Filters](core/filters.md)
2223
1. [Doctrine ORM Filters](core/filters.md#doctrine-orm-filters)
2324
1. [Search Filter](core/filters.md#search-filter)
2425
2. [Date Filter](core/filters.md#date-filter)
@@ -35,7 +36,7 @@
3536
3. [Creating Custom Filters](core/filters.md#creating-custom-filters)
3637
1. [Creating Custom Doctrine ORM Filters](core/filters.md#creating-custom-doctrine-orm-filters)
3738
2. [Overriding Extraction of Properties from the Request](core/filters.md#overriding-extraction-of-properties-from-the-request)
38-
6. [Serialization Groups and Relations](core/serialization-groups-and-relations.md)
39+
7. [Serialization Groups and Relations](core/serialization-groups-and-relations.md)
3940
1. [Configuration](core/serialization-groups-and-relations.md#configuration)
4041
2. [Using Different Serialization Groups per Operation](core/serialization-groups-and-relations.md#embedding-relations)
4142
3. [Embedding Relations](core/serialization-groups-and-relations.md#embedding-relations)
@@ -46,24 +47,24 @@
4647
5. [Entity Identifier Case](core/serialization-groups-and-relations.md#entity-identifier-case)
4748
6. [Writable Entity Identifier](core/serialization-groups-and-relations.md#writable-entity-identifier)
4849
7. [Embedding the Context](core/serialization-groups-and-relations.md#embedding-the-context)
49-
7. [Validation](core/validation.md)
50+
8. [Validation](core/validation.md)
5051
1. [Using Validation Groups](core/validation.md#using-validation-groups)
5152
2. [Dynamic Validation Groups](core/validation.md#dynamic-validation-groups)
52-
8. [Pagination](core/pagination.md)
53+
9. [Pagination](core/pagination.md)
5354
1. [Disabling the Pagination](core/pagination.md#disabling-the-pagination)
5455
2. [Changing the Number of Items per Page](core/pagination.md#changing-the-number-of-items-per-page)
55-
9. [The Event System](core/events.md)
56-
10. [Content Negotiation](core/content-negotiation.md)
56+
10. [The Event System](core/events.md)
57+
11. [Content Negotiation](core/content-negotiation.md)
5758
1. [Enabling Several Formats](core/content-negotiation.md#enabling-several-formats)
5859
2. [Registering a Custom Serializer](core/content-negotiation.md#registering-a-custom-serializer)
5960
3. [Creating a Responder](core/content-negotiation.md#creating-a-responder)
6061
4. [Writing a Custom Normalizer](core/content-negotiation.md#writing-a-custom-normalizer)
61-
11. [Using External JSON-LD Vocabularies](core/external-vocabularies.md)
62-
12. [Extending JSON-LD context](core/extending-jsonld-context.md)
63-
13. [Data Providers](core/data-providers.md)
62+
12. [Using External JSON-LD Vocabularies](core/external-vocabularies.md)
63+
13. [Extending JSON-LD context](core/extending-jsonld-context.md)
64+
14. [Data Providers](core/data-providers.md)
6465
1. [Custom Collection Data Provider](core/data-providers.md#creating-a-custom-data-provider#custom-collection-data-provider)
6566
2. [Custom Item Data Provider](core/data-providers.md#returning-a-paged-collection#custom-item-data-provider)
66-
14. [Extensions](core/extensions.md)
67+
15. [Extensions](core/extensions.md)
6768
1. [Custom Extension](core/extensions.md#custom-extension)
6869
2. [Filter upon the current user](core/extensions.md#example)
6970
16. [Security](core/security.md)

0 commit comments

Comments
 (0)