Skip to content

Commit 0b71dc0

Browse files
committed
docs(filters): promote QueryParameter usage over ApiFilter
The `ApiFilter` attribute is deprecated and scheduled for removal in API Platform 5.0. This commit updates the documentation to strongly recommend the modern `QueryParameter` syntax. This new approach offers better flexibility and more explicit, per-operation filter configuration. The examples have been updated to reflect this best practice.
1 parent b4f25bb commit 0b71dc0

File tree

3 files changed

+117
-6
lines changed

3 files changed

+117
-6
lines changed

core/doctrine-filters.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,59 @@
33
For further documentation on filters (including for Eloquent and Elasticsearch), please see the [Filters documentation](filters.md).
44

55
> [!WARNING]
6-
> Prefer using QueryParameter instead of ApiFilter for more flexibility, this is subject to change in the next major version.
6+
> For maximum flexibility and to ensure future compatibility, it is strongly recommended to configure your filters via
7+
> the parameters attribute using `QueryParameter`. The legacy method using the `ApiFilter` attribute is **deprecated** and
8+
> will be **removed** in version **5.0**.
9+
10+
The modern way to declare filters is to associate them directly with an operation's parameters. This allows for more
11+
precise control over the exposed properties.
12+
13+
Here is the recommended approach to apply a `PartialSearchFilter` only to the title and author properties of a Book resource.
14+
15+
```php
16+
<?php
17+
// api/src/Resource/Book.php
18+
19+
#[ApiResource(operations: [
20+
new GetCollection(
21+
parameters: [
22+
// This WILL restrict to only title and author properties
23+
'search[:property]' => new QueryParameter(
24+
properties: ['title', 'author'], // Only these properties get parameters created
25+
filter: new PartialSearchFilter()
26+
)
27+
]
28+
)
29+
])]
30+
class Book {
31+
// ...
32+
}
33+
```
34+
> [!TIP]
35+
> This filter can be also defined directly on a specific operation like `#[GetCollection(...)])` for finer
36+
> control, like the following code:
37+
38+
```php
39+
<?php
40+
// api/src/Resource/Book.php
41+
#[GetCollection(
42+
parameters: [
43+
// This WILL restrict to only title and author properties
44+
'search[:property]' => new QueryParameter(
45+
properties: ['title', 'author'], // Only these properties get parameters created
46+
filter: new PartialSearchFilter()
47+
)
48+
]
49+
)]
50+
class Book {
51+
// ...
52+
}
53+
```
54+
55+
**Further Reading**
56+
57+
- Consult the documentation on [Per-Parameter Filters (Recommended Method)](../core/filters.md#2-per-parameter-filters-recommended).
58+
- If you are working with a legacy codebase, you can refer to the [documentation for the old syntax (deprecated)](../core/filters.md#1-legacy-filters-searchfilter-etc---not-recommended).
759

860
## Basic Knowledge
961

@@ -112,7 +164,7 @@ class Offer
112164
}
113165
```
114166

115-
Learn more on how the [ApiFilter attribute](filters.md#apifilter-attribute) works.
167+
Learn more on how the [ApiFilter attribute](../core/filters.md#1-legacy-filters-searchfilter-etc---not-recommended) works.
116168

117169
For the sake of consistency, we're using the attribute in the below documentation.
118170

core/elasticsearch-filters.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,60 @@
22

33
For further documentation on filters (including for Eloquent and Doctrine), please see the [Filters documentation](filters.md).
44

5+
> [!WARNING]
6+
> For maximum flexibility and to ensure future compatibility, it is strongly recommended to configure your filters via
7+
> the parameters attribute using `QueryParameter`. The legacy method using the `ApiFilter` attribute is **deprecated** and
8+
> will be **removed** in version **5.0**.
9+
10+
The modern way to declare filters is to associate them directly with an operation's parameters. This allows for more
11+
precise control over the exposed properties.
12+
13+
Here is the recommended approach to apply a `MatchFilter` only to the title and author properties of a Book resource.
14+
15+
```php
16+
<?php
17+
// api/src/Resource/Book.php
18+
#[ApiResource(operations: [
19+
new GetCollection(
20+
parameters: [
21+
// This WILL restrict to only title and author properties
22+
'search[:property]' => new QueryParameter(
23+
properties: ['title', 'author'], // Only these properties get parameters created
24+
filter: new MatchFilter()
25+
)
26+
]
27+
)
28+
])]
29+
class Book {
30+
// ...
31+
}
32+
```
33+
> [!TIP]
34+
> This filter can be also defined directly on a specific operation like `#[GetCollection(...)])` for finer
35+
> control, like the following code:
36+
37+
```php
38+
<?php
39+
// api/src/Resource/Book.php
40+
#[GetCollection(
41+
parameters: [
42+
// This WILL restrict to only title and author properties
43+
'search[:property]' => new QueryParameter(
44+
properties: ['title', 'author'], // Only these properties get parameters created
45+
filter: new matchFilter()
46+
)
47+
]
48+
)]
49+
class Book {
50+
// ...
51+
}
52+
```
53+
54+
**Further Reading**
55+
56+
- Consult the documentation on [Per-Parameter Filters (Recommended Method)](../core/filters.md#2-per-parameter-filters-recommended).
57+
- If you are working with a legacy codebase, you can refer to the [documentation for the old syntax (deprecated)](../core/filters.md#1-legacy-filters-searchfilter-etc---not-recommended).
58+
559
## Ordering Filter (Sorting)
660

761
The order filter allows to [sort](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html)

core/filters.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Parameters and Filters
22

3+
For documentation on the specific filter implementations available for your persistence layer, please refer to the following pages:
4+
5+
- **[Doctrine Filters](../core/doctrine-filters.md)**
6+
- **[Elasticsearch Filters](../core/elasticsearch-filters.md)**
7+
38
API Platform provides a generic and powerful system to apply filters, sort criteria, and handle other request parameters. This system is primarily managed through **Parameter attributes** (`#[QueryParameter]` and `#[HeaderParameter]`), which allow for detailed and explicit configuration of how an API consumer can interact with a resource.
49

510
These parameters can be linked to **Filters**, which are classes that contain the logic for applying criteria to your persistence backend (like Doctrine ORM or MongoDB ODM).
@@ -8,10 +13,10 @@ You can declare parameters on a resource class to apply them to all operations,
813

914
<p align="center" class="symfonycasts"><a href="https://symfonycasts.com/screencast/api-platform/filters?cid=apip"><img src="../symfony/images/symfonycasts-player.png" alt="Filtering and Searching screencast"><br>Watch the Filtering & Searching screencast</a></p>
1015

11-
For documentation on the specific filter implementations available for your persistence layer, please refer to the following pages:
12-
13-
* [Doctrine Filters](../core/doctrine-filters.md)
14-
* [Elasticsearch Filters](../core/elasticsearch-filters.md)
16+
> [!WARNING]
17+
> For maximum flexibility and to ensure future compatibility, it is strongly recommended to configure your filters via
18+
> the parameters attribute using `QueryParameter`. The legacy method using the `ApiFilter` attribute is **deprecated** and
19+
> will be **removed** in version **5.0**.
1520
1621
## Declaring Parameters
1722

0 commit comments

Comments
 (0)