Skip to content

Commit 86536b4

Browse files
authored
Merge pull request #1173 from dunglas/fix/custom-filters
fix: modernize the docs regarding custom filters
2 parents 8811181 + 49cdff1 commit 86536b4

File tree

1 file changed

+64
-28
lines changed

1 file changed

+64
-28
lines changed

core/filters.md

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -905,74 +905,110 @@ final class RegexpFilter extends AbstractContextAwareFilter
905905
}
906906
```
907907

908-
Then, register this filter as a service:
908+
Thanks to [Symfony's automatic service loading](https://symfony.com/doc/current/service_container.html#service-container-services-load-example), which is enabled by default in the API Platform distribution, the filter is automatically registered as a service!
909909

910-
```yaml
911-
# api/config/services.yaml
912-
services:
913-
# ...
914-
'App\Filter\RegexpFilter':
915-
# Uncomment only if autoconfiguration isn't enabled
916-
#tags: [ 'api_platform.filter' ]
917-
```
910+
Finally, add this filter to resources you want to be filtered by using the `ApiFilter` annotation:
918911

919-
In the previous example, the filter can be applied on any property. However, thanks to the `AbstractFilter` class,
920-
it can also be enabled for some properties:
912+
```php
913+
<?php
914+
// api/src/Entity/Offer.php
921915
922-
```yaml
923-
# api/config/services.yaml
924-
services:
925-
'App\Filter\RegexpFilter':
926-
arguments: [ '@doctrine', ~, '@?logger', { email: ~, anOtherProperty: ~ } ]
927-
# Uncomment only if autoconfiguration isn't enabled
928-
#tags: [ 'api_platform.filter' ]
916+
namespace App\Entity;
917+
918+
use ApiPlatform\Core\Annotation\ApiFilter;
919+
use ApiPlatform\Core\Annotation\ApiResource;
920+
use App\Filter\RegexpFilter;
921+
922+
/**
923+
* @ApiResource
924+
* @ApiFilter(RegexpFilter::class)
925+
*/
926+
class Offer
927+
{
928+
// ...
929+
}
929930
```
930931

931-
Finally, add this filter to resources you want to be filtered:
932+
You can now use this filter in the URL like `http://example.com/offers?regexp_email=^[FOO]`. This new filter will also
933+
appear in OpenAPI and Hydra documentations.
934+
935+
In the previous example, the filter can be applied on any property. You can also apply this filter on a specific property:
932936

933937
```php
934938
<?php
935939
// api/src/Entity/Offer.php
936940
937941
namespace App\Entity;
938942
943+
use ApiPlatform\Core\Annotation\ApiFilter;
939944
use ApiPlatform\Core\Annotation\ApiResource;
940945
use App\Filter\RegexpFilter;
941946
942947
/**
943-
* @ApiResource(attributes={"filters"={RegexpFilter::class}})
948+
* @ApiResource
944949
*/
945950
class Offer
946951
{
947952
// ...
953+
954+
/**
955+
* @ApiFilter(RegexpFilter::class)
956+
*/
957+
public string $name;
948958
}
949959
```
950960

951-
Or by using the `ApiFilter` annotation:
961+
#### Manual Service and Attribute Registration
962+
963+
If you don't use Symfony's automatic service loading, you have to register the filter as a service by yourself.
964+
Use the following service definition (remember, by default, this isn't needed!):
965+
966+
```yaml
967+
# api/config/services.yaml
968+
services:
969+
# ...
970+
# This whole definition can be omitted if automatic service loading is enabled
971+
'App\Filter\RegexpFilter':
972+
# The "arguments" key can be omitted if the autowiring is enabled
973+
arguments: [ '@doctrine', ~, '@?logger' ]
974+
# The "tags" key can be omitted if the autoconfiguration is enabled
975+
tags: [ 'api_platform.filter' ]
976+
```
977+
978+
In the previous example, the filter can be applied on any property. However, thanks to the `AbstractFilter` class,
979+
it can also be enabled for some properties:
980+
981+
```yaml
982+
# api/config/services.yaml
983+
services:
984+
'App\Filter\RegexpFilter':
985+
arguments: [ '@doctrine', ~, '@?logger', { email: ~, anOtherProperty: ~ } ]
986+
tags: [ 'api_platform.filter' ]
987+
```
988+
989+
Finally, if you don't want to use the `@ApiFilter` annotation, you can register the filter on an API resource class using the `filters` attribute:
952990

953991
```php
954992
<?php
955993
// api/src/Entity/Offer.php
956994
957995
namespace App\Entity;
958996
959-
use ApiPlatform\Core\Annotation\ApiFilter;
960997
use ApiPlatform\Core\Annotation\ApiResource;
961998
use App\Filter\RegexpFilter;
962999
9631000
/**
964-
* @ApiResource
965-
* @ApiFilter(RegexpFilter::class)
1001+
* @ApiResource(
1002+
* attributes={
1003+
* "filters"={RegexpFilter::class}
1004+
* }
1005+
* )
9661006
*/
9671007
class Offer
9681008
{
9691009
// ...
9701010
}
9711011
```
972-
When using `ApiFilter` annotation, the declared properties in the `services.yaml` will not be taken into account. You have to use the `ApiFilter` way (see the [documentation](#apifilter-annotation)).
973-
974-
Finally you can use this filter in the URL like `http://example.com/offers?regexp_email=^[FOO]`. This new filter will also
975-
appear in Swagger and Hydra documentations.
9761012

9771013
### Creating Custom Doctrine MongoDB ODM Filters
9781014

0 commit comments

Comments
 (0)