Add search engine to Yii

Posted Apr 22, 2014

Edit controllers/PostController.php and add this to the function actionIndex:

	$criteria->addSearchCondition('title',$_GET['search'], true, 'AND');
	$criteria->addSearchCondition('content',$_GET['search'], true, 'OR');

Create components/Search.php with the following content:


class Search extends CPortlet
	public $title='Search';

	protected function renderContent()
		echo CHtml::form(Yii::app()->createUrl('post/index'), 'get');
		echo CHtml::textField('search', $_GET['search'],
			array('placeholder'=>'keyword...', 'size'=>20));
		echo CHtml::submitButton('OK');
		echo CHtml::endForm();

Now add this line to views/layouts/column2.php and you are all set!

<?php $this->widget('Search'); ?>

Leave a Comment

This will never be published.
You may use Markdown Syntax. HTML is not allowed.