ics/search-bundle

为 symfony 新增的包

0.0.2 2022-01-30 12:20 UTC

This package is auto-updated.

Last update: 2024-09-29 06:09:04 UTC


README

symfony 统一搜索

安装

确保全局已安装 Composer,如 Composer 文档的 安装章节 中所述。

使用 Symfony Flex 的应用程序

打开命令行,进入您的项目目录,执行以下命令:

composer require ics/search-bundle

不使用 Symfony Flex 的应用程序

步骤 1:下载 Bundle

打开命令行,进入您的项目目录,执行以下命令以下载此 Bundle 的最新稳定版本:

$ composer require ics/search-bundle

步骤 2:启用 Bundle

然后,通过将其添加到项目 config/bundles.php 文件中注册的 Bundle 列表中来启用该 Bundle

// config/bundles.php

return [
    // ...
    ICS\SearchBundle\SearchBundle::class => ['all' => true],
];

配置

路由

将以下行添加到 config/routes.yaml

search_bundle:
  resource: '@SearchBundle/config/routes.yaml'
  prefix: /search

使用

将实体集成到全局搜索中

1- 在您的实体中实现 EntitySearchInterface 接口。

# src/Entity/User.php

use ICS\SearchBundle\Entity\EntitySearchInterface;

class User implements EntitySearchInterface
{
    // Define name show in search results interface
    public static function getEntityClearName(): string
    {
        return "Application User";
    }
    // Define Template of the results
    public static function getSearchTwigTemplate(): string
    {
        return "search/result.html.twig";
    }
    // Define ROLES have access to the results
    public static function getRolesSearchEnabled(): array
    {
        return [
            'ROLE_ADMIN'
        ];
    }

}

2- 在实体仓库中实现 EntitySearchRepositoryInterface 接口。

# src/Repository/UserRepository.php

use ICS\SearchBundle\Entity\EntitySearchRepositoryInterface;

class UserRepository extends ServiceEntityRepository implements EntitySearchRepositoryInterface
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, User::class);
    }
    
    public function search(string $search) : ?Collection
    {
        $results = $this->createQueryBuilder('u')
            ->where('lower(u.name) LIKE lower(:search)')
            ->orWhere('lower(u.surname) LIKE lower(:search)')
            ->setParameter('search', "%".$search."%")
            ->orderBy('u.name', 'ASC')
            ->getQuery()
            ->getResult();
        return new ArrayCollection($results);
    }
  
}

3- 定义用于实体搜索结果的 HTML Twig。

每个结果实体都存储在 result 变量中。用户搜索词存储在 search 变量中。

{# templates/search/result.html.twig #}

<div class="card mb-3">
    <div class="row g-0">
        <div class="col-md-4">
            {% if result.avatar %}
                <img src="{{ result.avatar.thumbnails["mediaBundleMini"] }}" class="img-fluid rounded-start" alt="...">
            {% endif %}
        </div>
        <div class="col-md-8">
            <div class="card-body">
                <div class="btn-group float-end">
                    <a class="btn btn-warning btn-sm" href="{{ path('user-edit',{user: result.id}) }}">
                        <i class="fa fa-edit"></i>
                    </a>
                </div>
                <h5 class="card-title">{{ hightlight(result,search) }}</h5>
                <div class="text-muted">Gallery : {{ result.gallery|length }} elements</div>
            </div>
        </div>
    </div>
</div>

为了使用高亮显示返回一个 twig 函数,已经实现了一个功能,可以通过将搜索字段作为函数参数使用此功能。

    {# Highlight property ## name ## of result from ## search ## #}

    {{ hightlight(result.name,search) }}

    {# You can personalise the class of highlight #}
    
    {{ hightlight(result.name,search,'bg-success text-light') }}