Download the PHP package isma/api-filters-bundle without Composer
On this page you can find all versions of the php package isma/api-filters-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package api-filters-bundle
π― API Filters Bundle
A Symfony bundle that resolves API query filters from HTTP requests and applies them to query builders using a strategy pattern. Declare allowed filters with PHP attributes on your controller actions, and the bundle handles parsing, validation, and query building automatically.
π Requirements
- PHP >= 8.3
- Symfony 6.4 / 7.4 / 8.0+
- Doctrine ORM >= 3.4 (required for the built-in ORM filter strategies)
- Doctrine DBAL >= 4.0 (required for the built-in DBAL filter strategies)
- PDO (required for the built-in Pure SQL filter strategies β no Doctrine dependency)
π¦ Installation
If you're using Symfony Flex, the bundle is automatically registered. Otherwise, add it to config/bundles.php:
π Quick start
1. Add #[ApiFilter] attributes to your controller
2. Send a request with filters
Filters also support arrays (for eq, neq, like, ilike, inotlike, ieq, inoteq, start_with, end_with):
3. Apply filters to a Doctrine query builder
βοΈ The #[ApiFilter] attribute
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
string |
(required) | The filter field name used in the query string |
allowedTypes |
string[] |
[] (all types) |
Restrict which filter types can be used. Empty = all types allowed |
enumClass |
class-string<BackedEnum>\|null |
null |
Validate and cast the filter value against a backed enum |
Restricting filter types
If a client sends a disallowed type, a 400 Bad Request is returned.
Enum validation
The value is validated against the enum cases. Invalid values return a 400 Bad Request.
4. Apply filters using DBAL (without ORM)
If you don't use Doctrine ORM, you can use the DBAL filter applier with a raw Doctrine\DBAL\Query\QueryBuilder:
5. Apply filters using Pure SQL (PDO β no Doctrine required)
If your project doesn't use Doctrine at all, you can use the Pure SQL filter applier with a raw PDO connection:
π Built-in filter types
The bundle ships with 15 filter strategies for ORM, DBAL, and Pure SQL:
| Type | Query string | Scalar DQL | Array DQL |
|---|---|---|---|
eq |
filters[field][eq]=value |
field = :param |
field IN (:param) |
neq |
filters[field][neq]=value |
field != :param |
field NOT IN (:param) |
like |
filters[field][like]=value |
field LIKE '%val%' |
OR of LIKE clauses |
ilike |
filters[field][ilike]=value |
LOWER(field) LIKE LOWER('%val%') |
OR of LIKE clauses |
inotlike |
filters[field][inotlike]=value |
LOWER(field) NOT LIKE LOWER('%val%') |
AND of NOT LIKE clauses |
ieq |
filters[field][ieq]=value |
LOWER(field) = :param |
LOWER(field) IN (:param) |
inoteq |
filters[field][inoteq]=value |
LOWER(field) != :param |
LOWER(field) NOT IN (:param) |
start_with |
filters[field][start_with]=value |
field LIKE 'val%' |
OR of LIKE clauses |
end_with |
filters[field][end_with]=value |
field LIKE '%val' |
OR of LIKE clauses |
gt |
filters[field][gt]=value |
field > :param |
β throws exception |
gte |
filters[field][gte]=value |
field >= :param |
β throws exception |
lt |
filters[field][lt]=value |
field < :param |
β throws exception |
lte |
filters[field][lte]=value |
field <= :param |
β throws exception |
is_null |
filters[field][is_null]=true |
field IS NULL / field IS NOT NULL |
β throws exception |
order |
filters[field][order]=asc |
ORDER BY field ASC/DESC |
β throws exception |
Notes:
like,ilike,inotlike,start_with, andend_withautomatically escape%and_characters in user input.- Empty arrays are silently skipped for
eq,neq,ieq, andinoteq(no condition is added). ieqandinoteqonly accept string values (or arrays of strings) β passing a non-string value throws an\InvalidArgumentException.gt,gte,lt,lte,is_null, andorderonly accept scalar values β passing an array throws an\InvalidArgumentException.is_nullaccepts"true","1", ortruefor IS NULL, anything else for IS NOT NULL.orderaccepts only"asc"or"desc"(case-insensitive) and adds anORDER BYclause instead of aWHEREcondition.
π§© Creating a custom filter strategy
Implement FilterStrategyInterface and the bundle will auto-discover it:
The strategy is automatically tagged with isma_api_filters.strategy and collected by OrmFilterApplier. No manual service registration needed.
Creating a custom DBAL filter strategy
For DBAL, implement DbalFilterStrategyInterface:
The strategy is automatically tagged with isma_api_filters.dbal_strategy and collected by DbalFilterApplier.
Creating a custom Pure SQL filter strategy
For pure SQL (PDO), implement SqlFilterStrategyInterface:
The strategy is automatically tagged with isma_api_filters.sql_strategy and collected by SqlFilterApplier.
Naming convention: To avoid any collision with current or future built-in filter types, prefix your custom filter names with
x-(e.g.x-between,x-fulltext). This ensures your custom strategies will never conflict with a type added to the bundle in a later version.
You can then use it immediately:
β οΈ Error handling
| Scenario | Exception | HTTP status |
|---|---|---|
| Unknown filter field | InvalidFilterException |
400 |
| Disallowed filter type | InvalidFilterException |
400 |
| Invalid enum value | InvalidFilterException |
400 |
| Malformed filter format | InvalidFilterException |
400 |
Missing field mapping in apply() |
\InvalidArgumentException |
500 |
| Duplicate strategy for same type | DuplicateFilterStrategyException |
Container build error |
πΊοΈ Roadmap
- More filter types β Expand the built-in strategy catalog (e.g.
not_between,in_range,is_empty, full-text search, etc.).
π License
MIT - see LICENSE for details.
All versions of api-filters-bundle with dependencies
symfony/config Version ^6.4 || ^7.4 || ^8.0
symfony/dependency-injection Version ^6.4 || ^7.4 || ^8.0
symfony/http-foundation Version ^6.4 || ^7.4 || ^8.0
symfony/http-kernel Version ^6.4 || ^7.4 || ^8.0
doctrine/dbal Version ^4.0
doctrine/orm Version ^3.4