Download the PHP package monterhealth/api-filter-bundle without Composer
On this page you can find all versions of the php package monterhealth/api-filter-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download monterhealth/api-filter-bundle
More information about monterhealth/api-filter-bundle
Files in monterhealth/api-filter-bundle
Package api-filter-bundle
Short Description Rest Api filter and order bundle
License MIT
Informations about the package api-filter-bundle
MonterHealth ApiFilterBundle
MonterHealth ApiFilterBundle makes it easy to add filters and ordering to Rest API requests with full control within your Symfony Controller classes. Inspired by the filter system of API Platform but with its focus on usage within a controller.
Installation
Applications that use Symfony Flex
Open a command console, enter your project directory and execute:
Applications that don't use Symfony Flex
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
Usage
First define what combinations of parameters and filter typs can be used. This is possible by adding attributes to an entity class. Load the MonterHealth/ApiFilterBundle/MonterHealthApiFilter in your controller and pass it a Doctrine QueryBuilder, the entity's class name and the Request query. It will add all the constraints and orderings of the Request query to the QueryBuilder.
Entity
Add attributes to an entity class. You can add them at class or property level. The bundle can handle nested properties like author.name multiple levels deep.
Controller
Load the MonterHealth/ApiFilterBundle/MonterHealthApiFilter service into your controller using auto wiring.
Filters
Boolean filter
Query: parameter=<true|false|0|1>
For example:
/books?available=true
shows all books that are available.
Search filter
Available strategies:
- equals (default)
- partial
- start
- end
- word_start
- in
- null
Query: parameter[strategy]=value or parameter[][strategy] when setting multiple constraints on the same parameter. Add [not] before or after the strategy to get the opposite filter effect.
For example:
/books?author:name=agatha%20christie
returns all books where author.name equals 'agatha christie'.
/books?author[not][end]=rowling
returns all books where author doesn't end with 'rowling'.
/books?author:author[in]=agatha%20christie|j.k.%20rowling
returns all books where author matches 'agatha christie' or 'j.k. rowling'.
/books?title[][start]=harry&title[][not][word_start]=philosopher
returns all books that have a title that starts with 'harry' and where none of the words in the title start with 'philosopher'.
/books?title[null]
returns all books where the title is null. The null strategy doesn't need any extra value.
The in strategy includes the option to include NULL values. For example:
/books?author:id[in]=48|24|NULL
returns all books where the author id matches 48 or 24, and all books that have no author set.
/books?author:id[not][in]=48|24|NULL
returns all books where the author id doesn't match 48 and 24, and all books that have no author set.
As you may have noticed, nested properties must be referenced with a : sign like author:name in the uri.
Order filter
Available strategies:
- asc
- desc
Query: order_parameter_name[strategy]=parameter or order_parameter_name[][strategy]=parameter with multiple orderings.
For example:
/books?order=author:name
orders the list by author in the default direction (ascending) or in the direction set on the entity.
/books?order[desc]=author:name
orders the list by author in descending direction.
/books?order[][asc]=author:name&order[][asc]=title
orders the list by author in ascending direction and by title in ascending direction.
Numeric filter
Query: parameter=value
For example:
/books?stock=10
return all the books that have 10 stock.
Range filter
Available strategies:
- gt (greater than)
- lt (less than)
- gte (greater than equal)
- lte (less than equal)
- bt (between)
- equals
Query: parameter[strategy]=value OR parameter[][strategy]=value when setting multiple constraints on the same parameter.
For example:
/books?stock[lte]=10
return all the books which stock is less than equal to 10.
/books?stock[bt]=0|10
return all the books which stock is between 0 and 10.
Date filter
Available strategies:
- before (<=)
- after (>=)
- strictly_before (<)
- strictly_after (>)
- equals (=) (default)
- null_or_before
- null_or_after
- null_or_strictly_before
- null_or_strictly_after
- null
Query: parameter[strategy]=value OR parameter[][strategy]=value when setting multiple constraints on the same parameter.
For example:
books?createdAt[before]=2020-12-04
return all the books which createdAt <= 2020-12-04
books?createdAt[][after]=2020-12-01&createdAt[][before]=2020-12-04
return all the books which createdAt >= 2020-12-01 AND <= 2020-12-04
Configuration
Upgrade to version 2
To upgrade to version 2 it is advised to use rector. Add a configuration rule to convert the ApiFilter annotations to attributes: