You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/filters.md
+64-28Lines changed: 64 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -905,74 +905,110 @@ final class RegexpFilter extends AbstractContextAwareFilter
905
905
}
906
906
```
907
907
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!
909
909
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:
918
911
919
-
In the previous example, the filter can be applied on any property. However, thanks to the `AbstractFilter` class,
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:
952
990
953
991
```php
954
992
<?php
955
993
// api/src/Entity/Offer.php
956
994
957
995
namespace App\Entity;
958
996
959
-
use ApiPlatform\Core\Annotation\ApiFilter;
960
997
use ApiPlatform\Core\Annotation\ApiResource;
961
998
use App\Filter\RegexpFilter;
962
999
963
1000
/**
964
-
* @ApiResource
965
-
* @ApiFilter(RegexpFilter::class)
1001
+
* @ApiResource(
1002
+
* attributes={
1003
+
* "filters"={RegexpFilter::class}
1004
+
* }
1005
+
* )
966
1006
*/
967
1007
class Offer
968
1008
{
969
1009
// ...
970
1010
}
971
1011
```
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
0 commit comments