Simple Search MySql - uniqcle/Yii2 GitHub Wiki

Данный метод не учитывает релевантность, т.е вывод кол-ва запрошенных слов. Никак его не ранжирует. frontend\controllers\SearchController.php

namespace frontend\controllers; 

use Yii; 
use yii\web\Controller; 
use frontend\models\forms\SearchForm; 

class SearchController extends Controller 
{
	public function actionSimple(){

		$model = new SearchForm(); 

		$result = null; 

	 		if( $model->load( Yii::$app->request->post() ) ){

	 			$result = $model->simpleSearch(); 
			} 

		return $this->render('simple', [
			'model' => $model, 
			'result' => $result
		]); 
	}
}

frontend\models\forms\SearchForm.php

namespace frontend\models\forms;  

use yii\base\Model; 
use frontend\models\Search; 

class SearchForm extends Model 
{

	public $keyword; 

	public function rules(){
		return [
			['keyword', 'required'], 
			['keyword', 'trim']
		]; 
	}

 	public function simpleSearch(){

		if( $this->validate() ){

			$search = new Search(); 

			return $search->simpleSearch( $this->keyword ); 
		}
	} 
}

frontend\models\Search.php Занимается логикой поиска

namespace frontend\models; 

use Yii; 

 //Занимается логикой поиска
 class Search  
 {

 	public function simpleSearch($keyword){

 		$sql = "SELECT * FROM `news` WHERE content LIKE '%$keyword%' LIMIT 5; ";

 		return Yii::$app->db->createCommand($sql)->queryAll();  

 	}
 }

frontend\views\search\simple.php

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\LoginForm */
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use frontend\components\HighlightHelper; 

$this->title = 'Login User';
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="site-signup">
    <h1><?= Html::encode($this->title) ?></h1>

    <p></p>
	<!-- Форма ввода --> 
    <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>

                <?= $form->field($model, 'keyword')->textInput(['autofocus' => true]) ?>

                <div class="form-group">
                    <?= Html::submitButton('Search', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
                </div>

            <?php ActiveForm::end(); ?>
        </div>
    </div>

    <div class="row">
 	<!-- Область вывода --> 
    <?php if(is_array($result) && !empty($result)){ ?>
	<table class="table">
	  <thead class="thead-dark">
	    <tr>
	      <th scope="col">#</th>
	      <th scope="col">Title</th>
	      <th scope="col">Content</th>
	      <th scope="col">Status</th>
	    </tr>
	  </thead>
	  <tbody>
	<?php foreach($result as $item): ?>
	    <tr>
	      <th scope="row"><?=$item['id'] ?></th>
	      <td><?=$item['title']; ?></td>
	      <td><?php echo HighlightHelper::process($model->keyword, $item['content']);  ?></td>
	      <td><?=$item['status'];  ?></td>
	    </tr>
	<?php endforeach;  ?>

	  </tbody>
	</table>
	<?php } ?>		
				
     </div>
</div>

Компонент хэлпер frontend\components\HighlightHelper.php

namespace frontend\components; 

class HighlightHelper
{
	public static function process($string, $text){
		
		$newString = "<b>".$string."</b>"; 

		return str_replace($string,$newString,$text); 
	}
}
⚠️ **GitHub.com Fallback** ⚠️