CakePHP 3.0 简易搜索框架

安装: 63

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 59

类型:cakephp-plugin

1.2.3 2016-04-15 12:31 UTC

This package is not auto-updated.

Last update: 2024-09-19 19:17:44 UTC


README

Build Status Coverage Status Total Downloads License

Search 提供了一个 CakePHP 应用程序的搜索模块。

要求

主分支有如下要求

  • CakePHP 3.0.0 或更高版本。

安装

  • 使用 composer 在你的 CakePHP 项目的根目录(composer.json 文件所在位置)中安装插件
php composer.phar require friendsofcake/search
  • 通过在 config/bootstrap.php 中添加以下内容来加载插件
Plugin::load('Search');

或者运行命令

./bin/cake plugin load Search

用法

该插件有三个主要部分,你需要配置并包含在你的应用程序中。

表类

在你的表类设置过程中有三个任务。首先,你必须为 Search\Manager 添加一个 use 语句。接下来,你需要将 Search 行为附加到你的表类上。最后,你必须在你的表类中添加一个 searchConfiguration 方法,以便你可以配置搜索如何工作。

use Search\Manager;

class ExampleTable extends Table {

	public function initialize(array $config)
	{
		// Add the behaviour to your table
		$this->addBehavior('Search.Search');
	}

	// Configure how you want the search plugin to work with this table class
    public function searchConfiguration()
    {
        $search = new Manager($this)
            ->value('author_id', [
                'field' => $this->aliasField('author_id')
            ])
            // Here we will alias the 'q' query param to search the `Articles.title`
            // field and the `Articles.content` field, using a LIKE match, with `%`
            // both before and after.
            ->like('q', [
                'before' => true,
                'after' => true,
                'field' => [$this->aliasField('title'), $this->aliasField('content')]
            ])
            ->callback('foo', [
                'callback' => function ($query, $args, $manager) {
                    // Modify $query as required
                }
            ]);

        return $search;
    }

控制器类

为了使 Search 插件能够正常工作,它需要处理通过 URL 传递的查询参数。因此,你需要编辑你的 index 方法来适应这一点。

public function index()
{
    $query = $this->Articles
    	// Use the plugins 'search' custom finder and pass in the
    	// processed query params
        ->find('search', $this->Articles->filterParams($this->request->query))
        // You can add extra things to the query if you need to
        ->contain(['Comments'])
        ->where(['title IS NOT' => null]);

    $this->set('articles', $this->paginate($query));
}

search 查找器和 filterParams() 方法是由 Search 行为动态提供的。

组件

然后,将 Search Prg 组件添加到你的控制器中必要的函数。

⚠️ 注意:

  • 确保你将其添加到控制器的 initialize() 方法中。
  • 只将其添加到使用搜索的函数中,如你的 index() 方法。
public function initialize()
{
    parent::initialize();

    if ($this->request->action === 'index') {
        $this->loadComponent('Search.Prg');
    }
}

Search.Prg 组件将允许你的筛选表单使用查询参数中的数据填充。它使用 Post,重定向,获取模式

筛选数据

一旦完成所有设置,你现在可以通过在索引方法中传递查询参数来筛选数据。使用上面给出的 Article 示例,你可以使用以下方式筛选你的文章。

example.com/articles?q=cakephp

将筛选你的文章列表,以包含标题或内容字段中包含 "cakephp" 的任何文章。你可能会选择创建一个 get 表单,直接将筛选发送到 URL,但如果你在使用 Search.Prg 组件,你将想要使用 POST

创建你的表单

在大多数情况下,你会在索引视图中添加一个表单来搜索你的数据。

    echo $this->Form->create();
    // You'll need to populate $authors in the template from your controller
    echo $this->Form->input('author_id');
    // Match the search param in your table configuration
    echo $this->Form->input('q');
    echo $this->Form->button('Filter', ['type' => 'submit']);
    echo $this->Html->link('Reset', ['action' => 'index']);
    echo $this->Form->end();

如果你使用的是 Search.Prg 组件,表单的当前值将从查询参数中填充。

筛选器

Search 插件附带一系列预定义的搜索筛选器,允许你轻松创建所需的搜索结果。使用

  • value 限制结果为精确匹配
  • like 生成包含搜索查询的结果(LIKE
  • finder 使用一个 (自定义) 查找器生成结果
  • compare 生成需要操作符比较的结果(><>=<=
  • callback 使用你自己的自定义可调用函数生成结果

可选字段

有时你可能想要根据你的表单中的两个或三个输入来搜索你的数据。你可以使用 filterEmpty 搜索选项来忽略任何空字段。

// ExampleTable.php
// Inside your searchConfiguration() method
    $search->value('author_id', [
        'filterEmpty' => true
    ]);

确保在你的搜索表单中允许为空,如果你使用了一个。

echo $this->Form->input('author_id', ['empty' => 'Pick an author']);