black-lamp / yii2-search
Component for search in Active Record models
Installs: 203
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 1
Open Issues: 1
Type:yii2-extension
Requires
- yiisoft/yii2: ^2.0.4
Requires (Dev)
This package is not auto-updated.
Last update: 2024-10-26 19:53:54 UTC
README
Installation
Run command
composer require black-lamp/yii2-search
or add
"black-lamp/yii2-search": "^2.0.0"
to the require section of your composer.json.
Add 'SiteSearch' component to application config
'components' => [ // ... 'search' => [ 'class' => bl\search\SearchComponent::class, // models where you need the search 'models' => [ 'article' => [ 'class' => frontend\models\Article::class, 'label' => 'Articles' ], // ... ] ], ]
To models array you should to add the active record models where component will be make a search.
class
it's a model's name.
label
it's a title for displaying in view.
Implement interface in the models where you need a search
/** * @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' ]; } }
Using
Call method for getting a search results. This method return a SearchResult objects in array.
/** * @var \bl\search\data\SearchResult[] $result */ $result = Yii::$app->searcher->search('Black lamp'); foreach($result as $res) { $res->title; $res->description; $res->url; }