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
1. Through the resource declaration, as the `filters` attribute.
25
25
26
-
For example having a filter service declaration:
26
+
For example having a filter service declaration:
27
27
28
28
```yaml
29
29
# api/config/services.yaml
@@ -40,9 +40,9 @@ to a Resource in two ways:
40
40
public: false
41
41
```
42
42
43
-
We're linking the filter `offer.date_filter` with the resource like this:
43
+
We're linking the filter `offer.date_filter` with the resource like this:
44
44
45
-
[codeSelector]
45
+
[codeSelector]
46
46
47
47
```php
48
48
<?php
@@ -89,11 +89,11 @@ to a Resource in two ways:
89
89
</resources>
90
90
```
91
91
92
-
[/codeSelector]
92
+
[/codeSelector]
93
93
94
-
2. By using the `#[ApiFilter]` annotation.
94
+
2. By using the `#[ApiFilter]` attribute.
95
95
96
-
This annotation automatically declares the service, and you just have to use the filter class you want:
96
+
This attribute automatically declares the service, and you just have to use the filter class you want:
97
97
98
98
```php
99
99
<?php
@@ -113,12 +113,12 @@ to a Resource in two ways:
113
113
}
114
114
```
115
115
116
-
Learn more on how the [ApiFilter annotation](filters.md#apifilter-annotation) works.
116
+
Learn more on how the [ApiFilter attribute](filters.md#apifilter-attribute) works.
117
117
118
-
For the sake of consistency, we're using the annotation in the below documentation.
118
+
For the sake of consistency, we're using the attribute in the below documentation.
119
119
120
-
For MongoDB ODM, all the filters are in the namespace `ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter`. The filter
121
-
services all begin with `api_platform.doctrine_mongodb.odm`.
120
+
For MongoDB ODM, all the filters are in the namespace `ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter`. The filter
121
+
services all begin with `api_platform.doctrine_mongodb.odm`.
122
122
123
123
### Search Filter
124
124
@@ -815,7 +815,7 @@ services:
815
815
autowire: false
816
816
autoconfigure: false
817
817
public: false
818
-
818
+
819
819
# config/api/Offer.yaml
820
820
App\Entity\Offer:
821
821
# ...
@@ -927,7 +927,7 @@ class Tweet
927
927
services:
928
928
tweet.order_filter:
929
929
parent: 'api_platform.doctrine.orm.order_filter'
930
-
arguments:
930
+
arguments:
931
931
$properties: { id: ~, date: ~ }
932
932
$orderParameterName: 'order'
933
933
tags: [ 'api_platform.filter' ]
@@ -1230,7 +1230,7 @@ final class RegexpFilter extends AbstractContextAwareFilter
1230
1230
1231
1231
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!
1232
1232
1233
-
Finally, add this filter to resources you want to be filtered by using the `ApiFilter` annotation:
1233
+
Finally, add this filter to resources you want to be filtered by using the `ApiFilter` attribute:
1234
1234
1235
1235
```php
1236
1236
<?php
@@ -1303,7 +1303,7 @@ services:
1303
1303
tags: [ 'api_platform.filter' ]
1304
1304
```
1305
1305
1306
-
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:
1306
+
Finally, if you don't want to use the `#[ApiFilter]` attribute, you can register the filter on an API resource class using the `filters` attribute:
# Autoconfiguration must be disabled to set a custom priority
1537
-
autoconfigure: false
1538
-
```
1539
-
1540
-
It's key to set the priority higher than the `ApiPlatform\Core\EventListener\ReadListener`'s priority, as flagged in [this issue](https://github.com/api-platform/core/issues/1185), as otherwise the `PaginatorExtension` will ignore the Doctrine filter and return incorrect `totalItems` and `page` (first/last/next) data.
1541
-
1542
-
Lastly, implement the configurator class:
1543
-
1544
-
```php
1545
-
<?php
1546
-
// api/EventListener/UserFilterConfigurator.php
1547
-
1548
-
namespace App\EventListener;
1549
-
1550
-
use Symfony\Component\Security\Core\User\UserInterface;
1551
-
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1552
-
use Doctrine\ORM\EntityManagerInterface;
1553
-
use Doctrine\Common\Annotations\Reader;
1554
-
1555
-
final class UserFilterConfigurator
1556
-
{
1557
-
private $em;
1558
-
private $tokenStorage;
1559
-
private $reader;
1560
-
1561
-
public function __construct(EntityManagerInterface $em, TokenStorageInterface $tokenStorage, Reader $reader)
1562
-
{
1563
-
$this->em = $em;
1564
-
$this->tokenStorage = $tokenStorage;
1565
-
$this->reader = $reader;
1566
-
}
1567
-
1568
-
public function onKernelRequest(): void
1569
-
{
1570
-
if (!$user = $this->getUser()) {
1571
-
throw new \RuntimeException('There is no authenticated user.');
Done: Doctrine will automatically filter all "UserAware" entities!
1510
+
Done: Doctrine will automatically filter all `UserAware`entities!
1592
1511
1593
-
## ApiFilter Annotation
1512
+
## ApiFilter Attribute
1594
1513
1595
-
The annotation can be used on a `property` or on a `class`.
1514
+
The attribute can be used on a `property` or on a `class`.
1596
1515
1597
-
If the annotation is given over a property, the filter will be configured on the property. For example, let's add a search filter on `name` and on the `prop` property of the `colors` relation:
1516
+
If the attribute is given over a property, the filter will be configured on the property. For example, let's add a search filter on `name` and on the `prop` property of the `colors` relation:
1598
1517
1599
1518
```php
1600
1519
<?php
1601
1520
1602
1521
use ApiPlatform\Core\Annotation\ApiFilter;
1603
1522
use ApiPlatform\Core\Annotation\ApiResource;
1604
1523
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
On the first property, `name`, it's straightforward. The first annotation argument is the filter class, the second specifies options, here, the strategy:
1554
+
On the first property, `name`, it's straightforward. The first attribute argument is the filter class, the second specifies options, here, the strategy:
In the second annotation, we specify `properties` on which the filter should apply. It's necessary here because we don't want to filter `colors` but the `prop` property of the `colors` association.
1560
+
In the second attribute, we specify `properties` on which the filter should apply. It's necessary here because we don't want to filter `colors` but the `prop` property of the `colors` association.
1640
1561
Note that for each given property we specify the strategy:
The `ApiFilter` annotation can be set on the class as well. If you don't specify any properties, it'll act on every property of the class.
1567
+
The `ApiFilter` attribute can be set on the class as well. If you don't specify any properties, it'll act on every property of the class.
1647
1568
1648
1569
For example, let's define three data filters (`DateFilter`, `SearchFilter` and `BooleanFilter`) and two serialization filters (`PropertyFilter` and `GroupFilter`) on our `DummyCar` class:
1649
1570
@@ -1687,7 +1608,7 @@ The `DateFilter` given here will be applied to every `Date` property of the `Dum
0 commit comments