# Filters API Platform Core provides a generic system to apply filters on collections. Useful filters for the Doctrine ORM are provided with the library. You can also create custom filters that would fit your specific needs. You can also add filtering support to your custom [data providers](data-providers.md) by implementing interfaces provided by the library. By default, all filters are disabled. They must be enabled explicitly. When a filter is enabled, it is automatically documented as a `hydra:search` property in the collection response. It also automatically appears in the [NelmioApiDoc documentation](nelmio-api-doc.md) if it is available. ## Doctrine ORM Filters ### Search Filter If Doctrine ORM support is enabled, adding filters is as easy as registering a filter service in the `app/config/api_filters.yml` file and adding an attribute to your resource configuration. The search filter supports `exact`, `partial`, `start`, `end`, and `word_start` matching strategies: * `partial` strategy uses `LIKE %text%` to search for fields that containing the text. * `start` strategy uses `LIKE text%` to search for fields that starts with text. * `end` strategy uses `LIKE %text` to search for fields that ends with text. * `word_start` strategy uses `LIKE text% OR LIKE % text%` to search for fields that contains the word starting with `text`. Prepend the letter `i` to the filter if you want it to be case insensitive. For example `ipartial` or `iexact`. Note that this will use the `LOWER` function and **will** impact performance [if there is no proper index](performance.md#search-filter). Case insensitivity may already be enforced at the database level depending on the [collation](https://en.wikipedia.org/wiki/Collation) used. If you are using MySQL, note that the commonly used `utf8_unicode_ci` collation (and its sibling `utf8mb4_unicode_ci`) are already case insensitive, as indicated by the `_ci` part in their names. In the following example, we will see how to allow the filtering of a list of e-commerce offers: ```yaml # app/config/api_filters.yml services: offer.search_filter: parent: 'api_platform.doctrine.orm.search_filter' arguments: [ { id: 'exact', price: 'exact', name: 'partial' } ] tags: [ 'api_platform.filter' ] ``` ```php ]=value` The value can take any date format supported by the [`\DateTime` constructor](http://php.net/manual/en/datetime.construct.php). The `after` and `before` filters will filter including the value whereas `strictly_after` and `strictly_before` will filter excluding the value. As others filters, the date filter must be explicitly enabled: ```yaml # app/config/api_filters.yml services: # Enable date filter for for the property "dateProperty" of the resource "offer" offer.date_filter: parent: 'api_platform.doctrine.orm.date_filter' arguments: [ { dateProperty: ~ } ] tags: [ 'api_platform.filter' ] ``` ```php ` Enable the filter: ```yaml # app/config/api_filters.yml services: offer.order_filter: parent: 'api_platform.doctrine.orm.order_filter' arguments: [ { id: ~, name: ~ } ] tags: [ 'api_platform.filter' ] ``` ```php ` You can add as many groups as you need. Enable the filter: ```yaml # app/config/api_filters.yml services: book.group_filter: parent: 'api_platform.serializer.group_filter' arguments: # Default arguments values - 'groups' # The query parameter name - false # Allow to override the default serialization groups tags: [ 'api_platform.filter' ] ``` ```php ` You can add as many properties as you need. Enable the filter: ```yaml # app/config/api_filters.yml services: book.property_filter: parent: 'api_platform.serializer.property_filter' arguments: # Default arguments values - 'properties' # The query parameter name - false # Allow to override the default serialization properties tags: [ 'api_platform.filter' ] ``` ```php generateParameterName($property); // Generate a unique parameter name to avoid collisions with other filters $queryBuilder ->andWhere(sprintf('REGEXP(o.%s, :%s) = 1', $property, $parameterName)) ->setParameter($parameterName, $value); } // This function is only used to hook in documentation generators (supported by Swagger and Hydra) public function getDescription(string $resourceClass): array { $description = []; foreach ($this->properties as $property => $strategy) { $description['regexp_'.$property] = [ 'property' => $property, 'type' => 'string', 'required' => false, 'swagger' => [ 'description' => 'Filter using a regex. This will appear in the Swagger documentation!', 'name' => 'Custom name to use in the Swagger documentation', 'type' => 'Will appear below the name in the Swagger documentation', ], ]; } return $description; } } ``` Then, register this filter as a service: ```yaml # app/config/api_filters.yml services: 'AppBundle\Filter\RegexpFilter': tags: [ 'api_platform.filter' ] ``` In the previous example, the filter can be applied on any property. However, thanks to the `AbstractFilter` class, it can also be enabled for some properties: ```yaml # app/config/api_filters.yml services: 'AppBundle\Filter\RegexpFilter': arguments: [ '@doctrine', '@request_stack', '@?logger', { email: ~, anOtherProperty: ~ } ] tags: [ 'api_platform.filter' ] ``` Finally, add this filter to resources you want to be filtered: ```php query->get('filter[order]', []); } } ``` Finally, register the custom filter: ```yaml # app/config/api_filters.yml services: 'AppBundle\Filter\CustomOrderFilter': tags: [ 'api_platform.filter' ] ```