frontend/controllers/SiteController.php
/**
     * Search post by title and content
     *
     * @param string $s Keyword to search posts.
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
Search post by title and content
@param string $s Keyword to search posts.
@return string
@throws \yii\web\NotFoundHttpException
Search post by title and content @param string $s Keyword to search posts . @return string @throws \ yii \ web \ NotFoundHttpException
public function actionSearch($s)
    {
        $query = Post::find()
            ->orWhere(['like', 'title', $s])
            ->orWhere(['like', 'content', $s])
            ->andWhere(['status' => 'publish'])
            ->andWhere(['<=', 'date', date('Y-m-d H:i:s')])
            ->orderBy(['date' => SORT_DESC]);
        $countQuery = clone $query;
        $pages = new Pagination([
            'totalCount' => $countQuery->count(),
            'pageSize' => Option::get('posts_per_page'),
        ]);
        $query->offset($pages->offset)->limit($pages->limit);
        $posts = $query->all();
        if ($posts) {
            return $this->render('/site/search', [
                'posts' => $posts,
                'pages' => $pages,
                's' => $s,
            ]);
        }
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
    }
public function actionSearch ( $ s ) { $ query = Post :: find ( ) -> orWhere ( [ 'like' , 'title' , $ s ] ) -> orWhere ( [ 'like' , 'content' , $ s ] ) -> andWhere ( [ 'status' => 'publish' ] ) -> andWhere ( [ '<=' , 'date' , date ( 'Y-m-d H:i:s' ) ] ) -> orderBy ( [ 'date' => SORT_DESC ] ) ; $ countQuery = clone $ query ; $ pages = new Pagination ( [ 'totalCount' => $ countQuery -> count ( ) , 'pageSize' => Option :: get ( 'posts_per_page' ) , ] ) ; $ query -> offset ( $ pages -> offset ) -> limit ( $ pages -> limit ) ; $ posts = $ query -> all ( ) ; if ( $ posts ) { return $ this -> render ( '/site/search' , [ 'posts' => $ posts , 'pages' => $ pages , 's' => $ s , ] ) ; } throw new NotFoundHttpException ( Yii :: t ( 'writesdown' , 'The requested page does not exist.' ) ) ; }
Search post by title and content
 @param string $s Keyword to search posts.
Search post by title and content @param string $s Keyword to search posts .