Add search engine to Yii

Apr 22, 2014 by Johan

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'); ?>
Permalink · no comments yet · PHP, Yii · · Johan

Johan is a 37-year-old man from The Netherlands, currently living in Germany. His interests are music, universe & space, IT-technology, quantum mechanics, physics and electronics. After high school and college he has been working on IT-support for different companies. He is a fan of science fiction and is always excited to see technology evolve from idea to reality. Especially the developments for quantum computing to real life applications and new discoveries in space can always count on his attention.


Leave a Comment

This will never be published.
You may use Markdown Syntax. HTML is not allowed.
Click on the image to retrieve a new verification code.