PHPackages                             kazda01/yii2-search - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. kazda01/yii2-search

ActiveYii2-extension[Search &amp; Filtering](/categories/search)

kazda01/yii2-search
===================

A simple search engine that allows the user to search for models by defined attributes and rules.

21.8k↓38.1%PHPCI passing

Since Feb 24Pushed 1y ago2 watchersCompare

[ Source](https://github.com/kazda01/yii2-search)[ Packagist](https://packagist.org/packages/kazda01/yii2-search)[ RSS](/packages/kazda01-yii2-search/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Yii2 search
===========

[](#yii2-search)

A simple search engine that allows the user to search for models by defined attributes and rules.

[![Tests](https://github.com/kazda01/yii2-search/actions/workflows/tests.yml/badge.svg)](https://github.com/kazda01/yii2-search/actions/workflows/tests.yml)[![Total Downloads](https://camo.githubusercontent.com/28a94b6a5fc00b1f423a6e646c5d9aba16afbeb40a842e9462cd00edd6ba6271/68747470733a2f2f706f7365722e707567782e6f72672f6b617a646130312f796969322d7365617263682f646f776e6c6f616473)](https://packagist.org/packages/kazda01/yii2-search)[![License](https://camo.githubusercontent.com/66e1c22994290283d99bba9594a595a35813d63bd6f4e0f33e45060664155cdb/68747470733a2f2f706f7365722e707567782e6f72672f6b617a646130312f796969322d7365617263682f6c6963656e7365)](https://packagist.org/packages/kazda01/yii2-search)

[![Search example](docs/search-example.gif)](docs/search-example.gif)

After specifying the rules, the user can search for multiple Models by multiple attributes at once. The results are then dynamically listed under the input. For each search result, its IdentifyingString and the name of the attribute in which a match was found is displayed. The exact match is also highlighted in bold.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

```
composer require kazda01/yii2-search "@dev"

```

Usage
-----

[](#usage)

### 1. Add Module and configure search parameters

[](#1-add-module-and-configure-search-parameters)

After adding the project via composer, you need to add the SearchModule in the `web.php` modules and configure searching parameters. If you want to have multiple different search engines (e.g. Invoice and User search engine), add the Module multiple times, ModuleID will be used for distinguishing.

The ModuleID will also be used to create the search route. The final requests will go to `website.com/ModuleID/search?search=query`. It is important to add the ModuleID to bootstrap so that the route is added to the UrlManager with priority over your site's routes (this action is not needed if your routes do not conflict with the search route).

```
return [
    ...
    'bootstrap' => [''],
    'modules' => [
        '' => [
            'class' => '\kazda01\search\SearchModule',
            'searchConfig' => [
                '' => [
                    'columns' => ['', ''],
                    'matchTitle' => '',
                    'matchText' => function($model){
                        return 'Model: ' . $model->id;
                    }
                ],
            ]
        ],
    ],
    ...
];
```

All available settings are described here.

### 2. Add Search Model classes

[](#2-add-search-model-classes)

Search Model classes must be generated. They can be simply made by [Gii CRUD generator](https://www.yiiframework.com/doc/guide/2.0/en/start-gii#generating-crud). You can restrict what results certain users can search/display.

### 3. Generate search input

[](#3-generate-search-input)

In a view, add this line.

```
