CakePHP Search Function

Providing a search function that searches your database is a strategy that offers users a way to find content. Users can locate content by searching for specific name phrases, without needing to understand or navigate through the structure of the Web application system. In this case, the search function will do searching based on the username. This search function is develop using the plugin developed by CakeDevelopment Corporation (CakeDC) where this plugin is free and available to be download from Github website.
Download plugin from Github link: (download as zip archive).
search 1
Extract zip file into C:\wamp\www\appfolder\app\Plugin and rename default folder name “search-master” to “Search”.
The plugin need to be load into CakePHP environment before it can be use in the system development. To load the search plugin, navigate to …\appfolder\app\Config and open file bootstrap.php and insert load plugin code as shown below (Mostly at last line):
CakeLog::config('error', array(
	'engine' => 'FileLog',
	'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
	'file' => 'error',



In CakePHP, it is related to the MVC where each of the function has controller, model and view. In this case, the example is to search users based on username. This means the search function will find any matching keyword from username field in users table and list the record in table format. Navigate to “…\appfolder\app\Controller” and open file UsersController.php and add this code:
class UsersController extends AppController {
public $paginate = array();  //create array for record page
public $components = array('Search.Prg');
public $presetVars = true;  // using the model configuration
public function search() {
        $this->paginate['conditions'] = $this->User->parseCriteria($this->passedArgs);
        $this->set('users', $this->paginate());


The model for user also should be modified which is to include the filtering function in order for search plugin to work. Navigate to “…\appfolder\app\Model” and open file User.php and add:
class User extends AppModel {
public $actsAs = array('Search.Searchable');
public $filterArgs = array(
            'username' => array('type' => 'query', 'method' => 'filterUsername'),
        public function filterUsername($data, $field = null) {
            if (empty($data['username'])) {
                return array();
            $usernameField = '%' . $data['username'] . '%';
            return array(
                'OR' => array(
                    $this->alias . '.username LIKE' => $usernameField,


The search box will reside in search.ctp page. This page currently not exist but it can be generated as the code in this page are exactly the same code like index.ctp for user views that can be found in “…\appfolder\app\View\Users”. To have the search box, add below code into the search.ctp page (depend on where the box should appear):
echo $this->Form->create('User');
echo $this->Form->input('username', array('div' => false));
echo $this->Form->submit(__('Search'), array('div' => false));
echo $this->Form->end();


Navigate to appname/users/search to access the search form. Good Luck!
Author Information
Muhammad Asyraf bin Wahi Anuar
Web Administrator
Asyraf enjoys life and has great enthusiasm for all outdoors activities especially MTB! Don't Follow Me. I'm Lost Too -_-

Muhammad Asyraf bin Wahi Anuar | Copyright 2017 | Informatic Trooper