Download the PHP package darxide-pl/cakephp-filter without Composer
On this page you can find all versions of the php package darxide-pl/cakephp-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download darxide-pl/cakephp-filter
More information about darxide-pl/cakephp-filter
Files in darxide-pl/cakephp-filter
Package cakephp-filter
Short Description Filtering Queries plugin with extended FormHelper for CakePHP
License MIT
Informations about the package cakephp-filter
Filter plugin for CakePHP
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
Intro
In real life applications, filtering queries by user input can quickly growth into enormous queries controlled by tons of inputs.
The main idea of this plugin is keep the code clean and readable.
Every query can have many filters as separated methods in App\Model\Filter
namespace, and queries are passed into these filters as parameters, so filters are easy to test and maintain.
Plugin allows you to filtering queries using get variables, post variables, json input.
Plugin has also implemented "memory" of filter in session (optional) and has easily extendable interfaces to help You with write your own implementations.
There are also FilterForm
Helper which helps with building forms with filters.
Quick example
- cmd:
./bin/cake plugin load Filter
-
Load helper in
/src/View/AppView.php
-
Use
Filter\FilterAwareTrait
in YourTable
class (notice: this requires the creation of a table or just use existing one) (notice2: Traits needs to declareuse
insideclass
declatarion) example file is/src/Model/Table/UsersTable.pgp
-
Create file named
UsersFilter.php
in/src/Model/Filter/
-
Now, we can bind that filter to query in controller and|or paginate this query (in example
/src/Controller/PagesController.php
) - The last step is html form template. Create
/src/Template/Pages/filter_example.ctp
file
Docs
1. Use cases
1.1 Filtering same query for different user types
Sometimes we need to disable some fields in filters for different user types.
We can realize that in many ways, and we should consider some possibilities.
We can create separated filter classes for each type of users if filter is complicated and differences between forms for user and admin are huge:
/src/Model/Filter/Users/AdminsFilter.php
/src/Model/Filter/Users/CustomersFilter.php
Then in controller we can bind expected filter to query as in a quick example.
Notice: Two lines above, we moved filter class one level deeper into a folder tree. The only one difference is argument passed to create FormFilter which should look like this:
or
If difference between user types is small, You can use Session inside filter:
In our /src/Model/Filter/UsersFilter.php
:
1.2 Get value of one filter method inside another
We can simple realize that by using getter: FilterInterface::get(string $name)
1.3 Access to database inside filter
I think we should avoid that operation, but it happens. Sometimes before filtering we need to fetch some data from database only for preparing the final filtered query.