@@ -10,7 +10,9 @@ By default, all filters are disabled. They must be enabled explicitly.
10
10
When a filter is enabled, it is automatically documented as a ` hydra:search ` property in the collection response. It also
11
11
automatically appears in the [ NelmioApiDoc documentation] ( nelmio-api-doc.md ) if it is available.
12
12
13
- ## Search Filter
13
+ ## Doctrine ORM Filters
14
+
15
+ ### Search Filter
14
16
15
17
If Doctrine ORM support is enabled, adding filters is as easy as registering a filter service in your ` app/config/services.yml `
16
18
file and adding an attribute to your resource configuration.
@@ -82,7 +84,7 @@ Using a numeric ID is also supported: `http://localhost:8000/api/offers?product=
82
84
83
85
Previous URLs will return all offers for the product having the following IRI as JSON-LD identifier (`@id`) : ` http://localhost:8000/api/products/12` .
84
86
85
- # # Date Filter
87
+ # ## Date Filter
86
88
87
89
The date filter allows to filter a collection by date intervals.
88
90
@@ -121,7 +123,7 @@ class Offer
121
123
}
122
124
` ` `
123
125
124
- # ## Managing `null` Values
126
+ # ### Managing `null` Values
125
127
126
128
The date filter is able to deal with date properties having `null` values.
127
129
Four behaviors are available at the property level of the filter :
@@ -148,7 +150,7 @@ services:
148
150
If you use a service definition format other than YAML, you can use the `ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter::EXCLUDE_NULL`
149
151
constant directly.
150
152
151
- # # Boolean Filter
153
+ # ## Boolean Filter
152
154
153
155
The boolean filter allows you to search on boolean fields and values.
154
156
@@ -190,7 +192,7 @@ Given that the collection endpoint is `/offers`, you can filter offers by boolea
190
192
191
193
It will return all offers where `isAvailableGenericallyInMyCountry` equals `true`.
192
194
193
- # # Numeric Filter
195
+ # ## Numeric Filter
194
196
195
197
The numeric filter allows you to search on numeric fields and values.
196
198
@@ -231,7 +233,7 @@ Given that the collection endpoint is `/offers`, you can filter offers by boolea
231
233
It will return all offers with `sold` equals 1
232
234
233
235
234
- # # Range Filter
236
+ # ## Range Filter
235
237
236
238
The range filter allows you to filter by a value Lower than, Greater than, Lower than or equal, Greater than or equal and between two values.
237
239
@@ -273,7 +275,7 @@ It will return all offers with `price` between 12.99 and 15.99.
273
275
274
276
You can filters offers by joining two value for example : ` /offers?price[gt]=12.99&price[lt]=19.99` .
275
277
276
- # # Order Filter
278
+ # ## Order Filter
277
279
278
280
The order filter allows to order a collection against the given properties.
279
281
@@ -325,7 +327,7 @@ services:
325
327
tags: [ { name: 'api_platform.filter', id: 'offer.order' } ]
326
328
` ` `
327
329
328
- # ## Comparing with Null Values
330
+ # ### Comparing with Null Values
329
331
330
332
When the property used for ordering can contain `null` values, you may want to specify how `null` values are treated in
331
333
the comparison :
@@ -351,7 +353,7 @@ services:
351
353
If you use a service definition format other than YAML, you can use the `ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter::NULLS_SMALLEST`
352
354
constant directly.
353
355
354
- # ## Using a Custom Order Query Parameter Name
356
+ # ### Using a Custom Order Query Parameter Name
355
357
356
358
A conflict will occur if `order` is also the name of a property with the search filter enabled.
357
359
Luckily, the query parameter name to use is configurable :
@@ -364,7 +366,7 @@ api_platform:
364
366
order_parameter_name: '_order' # the URL query parameter to use is now "_order"
365
367
` ` `
366
368
367
- # # Filtering on Nested Properties
369
+ # ## Filtering on Nested Properties
368
370
369
371
Sometimes, you need to be able to perform filtering based on some linked resources (on the other side of a relation). All
370
372
built-in filters support nested properties using the dot (`.`) syntax, e.g. :
@@ -387,7 +389,7 @@ services:
387
389
The above allows you to find offers by their respective product's color : ` http://localhost:8000/api/offers?product.color=red` ,
388
390
or order offers by the product's release date : ` http://localhost:8000/api/offers?order[product.releaseDate]=desc`
389
391
390
- # # Enabling a Filter for All Properties of a Resource
392
+ # ## Enabling a Filter for All Properties of a Resource
391
393
392
394
As we have seen in previous examples, properties where filters can be applied must be explicitly declared. If you don't
393
395
care about security and performance (e.g. an API with restricted access), it is also possible to enable built-in filters
@@ -417,6 +419,144 @@ It means that the filter will be **silently** ignored if the property:
417
419
* is not enabled
418
420
* has an invalid value
419
421
422
+ # # Serializer Filters
423
+
424
+ # ## Group Filter
425
+
426
+ The group filter allows you to filter by serialization groups.
427
+
428
+ Syntax : ` ?groups[]=<group>`
429
+
430
+ You can add as many groups as you need.
431
+
432
+ Enable the filter :
433
+
434
+ ` ` ` yaml
435
+ # app/config/services.yml
436
+
437
+ services:
438
+ book.group_filter:
439
+ parent: 'api_platform.serializer.group_filter'
440
+ arguments: # Default arguments values
441
+ - 'groups' # The query parameter name
442
+ - false # Allow to override the default serialization groups
443
+ tags: [ { name: 'api_platform.filter', id: 'book.group' } ]
444
+ ` ` `
445
+
446
+ ` ` ` php
447
+ <?php
448
+
449
+ // src/AppBundle/Entity/Book.php
450
+
451
+ namespace AppBundle\E ntity;
452
+
453
+ use ApiPlatform\C ore\A nnotation\A piResource;
454
+
455
+ /**
456
+ * @ApiResource(attributes={"filters"={"book.group"}})
457
+ */
458
+ class Book
459
+ {
460
+ // ...
461
+ }
462
+ ` ` `
463
+
464
+ Given that the collection endpoint is `/books`, you can filter by serialization groups with the following query : ` /books?groups[]=read&groups[]=write` .
465
+
466
+ By default the query parameter name is `groups` but you can easily customize it to another by changing the first argument.
467
+ For example, with `serialization_groups` as name :
468
+
469
+ ` ` ` yaml
470
+ # app/config/services.yml
471
+
472
+ services:
473
+ book.group_filter:
474
+ parent: 'api_platform.serializer.group_filter'
475
+ arguments: [ 'serialization_groups' ]
476
+ tags: [ { name: 'api_platform.filter', id: 'book.group' } ]
477
+ ` ` `
478
+
479
+ You can also override the default serialization groups when you use the filter by changing the second argument to
480
+ `true` :
481
+
482
+ ` ` ` yaml
483
+ # app/config/services.yml
484
+
485
+ services:
486
+ book.group_filter:
487
+ parent: 'api_platform.serializer.group_filter'
488
+ arguments: [ 'groups', true ]
489
+ tags: [ { name: 'api_platform.filter', id: 'book.group' } ]
490
+ ` ` `
491
+
492
+ # ## Property filter
493
+
494
+ The property filter add the possibility to filter serialization properties.
495
+
496
+ Syntax : ` ?properties[]=<property>`
497
+
498
+ You can add as many properties as you need.
499
+
500
+ Enable the filter :
501
+
502
+ ` ` ` yaml
503
+ # app/config/services.yml
504
+
505
+ services:
506
+ book.property_filter:
507
+ parent: 'api_platform.serializer.property_filter'
508
+ arguments: # Default arguments values
509
+ - 'properties' # The query parameter name
510
+ - false # Allow to override the default serialization properties
511
+ tags: [ { name: 'api_platform.filter', id: 'book.property' } ]
512
+ ` ` `
513
+
514
+ ` ` ` php
515
+ <?php
516
+
517
+ // src/AppBundle/Entity/Book.php
518
+
519
+ namespace AppBundle\E ntity;
520
+
521
+ use ApiPlatform\C ore\A nnotation\A piResource;
522
+
523
+ /**
524
+ * @ApiResource(attributes={"filters"={"book.property"}})
525
+ */
526
+ class Book
527
+ {
528
+ // ...
529
+ }
530
+ ` ` `
531
+
532
+ Given that the collection endpoint is `/books`, you can filter the serialization properties with the following query : ` /books?properties[]=title&properties[]=author` .
533
+
534
+ By default the query parameter name is `properties` but you can easily customize it to another by changing the first argument.
535
+ For example, with `serialization_properties` as name :
536
+
537
+ ` ` ` yaml
538
+ # app/config/services.yml
539
+
540
+ services:
541
+ book.property_filter:
542
+ parent: 'api_platform.serializer.property_filter'
543
+ arguments: [ 'serialization_properties' ]
544
+ tags: [ { name: 'api_platform.filter', id: 'book.property' } ]
545
+ ` ` `
546
+
547
+ You can also override the default serialization properties when you use the filter by changing the second argument to
548
+ `true` :
549
+
550
+ ` ` ` yaml
551
+ # app/config/services.yml
552
+
553
+ services:
554
+ book.property_filter:
555
+ parent: 'api_platform.serializer.property_filter'
556
+ arguments: [ 'properties', true ]
557
+ tags: [ { name: 'api_platform.filter', id: 'book.property' } ]
558
+ ` ` `
559
+
420
560
# # Creating Custom Filters
421
561
422
562
Custom filters can be written by implementing the `ApiPlatform\Core\Api\FilterInterface`
0 commit comments