We’ve just released version 2.3.0 of the CakeDC Search plugin for CakePHP with an array of fixes and improvements, updated the code for 2.5, and also reformatted the documentation inline with the CakeDC Plugin Standard.
The Search plugin provides an interface using PRG (Post/Redirect/Get) to enable search in any CakePHP application. From simple comparisons of values, to complex search types on any data, even wildcards, expression and custom query methods, as well as subqueries. The plugin gives you all you need, in just a few lines of code.
As an example, if you had a controller for products, and wanted to offer a way to simply search among your existing products by “name”, you'd need only include the Prg component, then use it to process the request for the search criteria. Here we show the most verbose example, so it's clear how the component is used.
class ProductsController extends AppController { public $components = array('Search.Prg'); public function index() { // start a standard search $this->Prg->commonProcess(); // process the URL parameters $params = $this->Prg->parsedParams(); // generate the Paginator conditions $conditions = $this->Product->parseCriteria($params); // add the conditions for paging $this->Paginator->settings['conditions'] = $conditions; $this->set('products', $this->Paginator->paginate()); } }
Then, in your product model, you would just include a $filterArgs property, which defines the fields by which you can search for records. In this case, you can see how the “name” field is defined as a searchable column.
class Product extends AppModel { public $actsAs = array('Search.Searchable'); public $filterArgs = array( 'product' => array( 'type' => 'like', 'field' => 'name' ) ); }
Finally, in your view, there's no need to do any more than define your search form as you normally would, without the need for any additional options or configuration.
echo $this->Form->create(); echo $this->Form->input('product'); echo $this->Form->end(__('Search'));
This is just a simple example, to illustrate the potential of the Search plugin. You can find more on the functionality available in the documentation. We're sure you'll find plenty more uses for it, to enhance your application with a search functionality that will keep your users searching for more!
As you already know, our plugins are released as Open Source, free of charge, and are helped by the contributions from the CakePHP community. We thank all of our contributors to the Search plugin, and hope the continued involvement helps keep the framework ecosystem healthy and strong, with an array of great plugins that help you all build awesome applications.