PHP code example of black-lamp / yii2-search

1. Go to this page and download the library: Download black-lamp/yii2-search library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

black-lamp / yii2-search example snippets


'components' => [
      // ...
      'search' => [
            'class' => bl\search\SearchComponent::class,
            // models where you need the search
            'models' => [
                'article' => [
                    'class' => frontend\models\Article::class,
                    'label' => 'Articles'
                 ],
                // ...
            ]
      ],
]

/**
 * @property integer $id
 * @property string $title
 * @property string $shortText
 * @property string $fullText
 * @property string $socialNetworksText
 */
class Article extends ActiveRecord implements \bl\search\interfaces\SearchInterface
{
    // ...

    /**
     * @inheritdoc
     */
    public function getSearchTitle() {
        return $this->title;
    }

    /**
     * @inheritdoc
     */
    public function getSearchDescription() {
        return $this->shortText;
    }

    /**
     * @inheritdoc
     */
    public function getSearchUrl() {
       return Url::toRoute["/articles/$this->id"];
    }

   /**
    * @inheritdoc
    */
    public function getSearchFields() {
        return [
            'title',
            'shortText',
            'fullText'
        ];
    }
}

/**
 * @var \bl\search\data\SearchResult[] $result
 */
$result = Yii::$app->searcher->search('Black lamp');

foreach($result as $res) {
    $res->title;
    $res->description;
    $res->url;
}