i try to make a search form in my application, i want search the post with one input and his search on title and content.
EDIT : I use the CakePHP Search plugin My controller
public function initialize(){
parent::initialize();
$this->loadComponent('Search.Prg', ['actions'=>'index','lookup']);
}
public function search(){
$query = $this->Posts
->find('search',['search' => $this->request->query])
->contain(['Users','Categories'])
->where(['Posts.status' => 1]);
$this->set(compact('posts', $this->paginate($query)));
}
My Model
use Search\Manager;
$this->addBehavior('Search.Search');
$this->searchManager()
->add('q', 'Search.Like', [
'before' => true,
'after' => true,
'mode' => 'or',
'comparison' => 'LIKE',
'wildcardAny' => '*',
'wildcardOne' => '?',
'field' => [$this->aliasField('name'), $this->aliasField('content')]
]);
And in my view
<?= $this->Form->create(); ?>
<?= $this->Form->input('q'); ?>
<?= $this->Form->button('Search', ['action'=>'index']); ?>
<?= $this->Form->end(); ?>
Now, how show the results of query ?
To display the search result do the following "additional" things: 1st: Load your component in AppController
2nd: Add the following code in your search method:
3rd: edit your search.ctp file to view the result: