Add search engine to Yii

Gepostet 22.04.2014

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

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

Create components/Search.php with the following content:

<?php
Yii::import('zii.widgets.CPortlet');

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'); ?>

Schreibe einen Kommentar

Dies wird niemals veröffentlicht werden.
Sie können Markdown-Syntax verwenden. HTML ist nicht erlaubt.