gkratz / searchbundle
一个易于将搜索引擎集成到您自己的 symfony 3 数据中的包
dev-master / dev-master
2017-04-14 08:33 UTC
Requires
- php: >=5.5.0
- doctrine/doctrine-bundle: *
- knplabs/knp-paginator-bundle: ^2.5
- symfony/framework-bundle: >=3.0.1
- twig/twig: *
This package is auto-updated.
Last update: 2020-05-26 01:22:40 UTC
README
1) //-- 在您的项目 "composer.json" 中添加以下内容 --//
"require": {
// ...
"gkratz/searchbundle": "dev-master"
}
2) //-- 在您的 "app\AppKernel.php" 中激活该包 --//
new Gkratz\SearchBundle\GkratzSearchBundle(),
new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
3) //-- 在终端中输入以下命令,安装依赖时不要担心 "Undefined index: search" 警告。错误将在安装的其他步骤中修正 --//
php composer update gkratz/searchbundle
4) //-- 更新您的 "app\config\config.yml" 文件 --//
# KNPPaginatorBundle Configuration
knp_paginator:
default_options:
distinct: false
template:
# pagination: YourCustomBundle:pagination.html.twig
# GkratzSearchBundle Configuration
gkratz_search:
search:
allow_approaching: true # may be very slow. set false to only allow perfect match.
fields: Test.title, Test.content # entity must exists if you let it!!!!!!!!!!!!!!!!!!!!!!!
5) //-- 确保以下 "app\config\config.yml" 中的行取消注释以启用翻译 --//
framework:
translator: { fallbacks: ["%locale%"] }
6) //-- 更新您的 "app\config\routing.yml" 文件 --//
#this route must ABSOLUTELY be
#at the top of the config.yml file
#LINE 1
GkratzSearch:
resource: "@GkratzSearchBundle/Controller/"
type: annotation
prefix: /search
7) //-- 生成扩展搜索模型的搜索实体 --//
php bin/console doctrine:generate:entity
//-- 快捷名称:AppBundle:Search --//
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gkratz\SearchBundle\Model\Search as BaseSearch;
/**
*
* @ORM\Entity(repositoryClass="AppBundle\Repository\SearchRepository")
* @ORM\Table(name="search")
*/
class Search extends BaseSearch
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
}
8) //-- 更新您的 "主布局" --//
<form action="{{ path('gkratz_search_search_search') }}" method="post">
{{ render(controller('GkratzSearchBundle:Search:index', {request: app.request})) }}
</form>
9) //-- 在终端中输入以下命令 --//
php bin/console doctrine:generate:entities App
php bin/console doctrine:schema:update --force
使用方法
- you can change limit results per page and maximum size of search results in Gkartz\AdminBundle\Constants\Constants.php
- to use the search plugin, you need to activate the fields for the search in the config.yml and
# config.yml
# GkratzAdminBundle Configuration
gkratz_admin:
search:
allow_approaching: true # may be very slow. set false to only allow perfect match.
fields: Post.title, Post.content, User.username, User.lastname, User.firstname, User.locality, User.country
# ...
覆盖
- you can easily overwrite translations, controllers and views. The bundle is KISS made, simple and powerful.
- to overwrite results views, make a GkratzSearchBundle/views/index.html.twig file in app/Resources -> ie:
{% extends 'base.html.twig' %}
{% block body %}
<div>
<h1>{{ 'Search results' | trans }} - {{ entities.getTotalItemCount }}</h1>
<h2>{{ entities.0 is defined ? entities.0.searchText | capitalize : '' }}</h2>
<div>
{% if approach is defined %}
<a href="{{ path('gkratz_search_search_search', {approaching: 1, search: app.request.get('id')}) }}"> {{ 'Expand search' | trans }}</a>
{% endif %}
</div>
<section class="row">
{% for entity in entities %}
<a href="yourcustomlinkhere">
<h2>#{{ loop.index }} {{ entity.class | capitalize }} id {{ entity.elementId }}</h2>
<div>
{{ entity.resultText | searchtext(entity.searchText, allowApproaching) }}
</div>
</a>
{% else %}
<p class="error">{{ "No entities" | trans }}</p>
{% endfor %}
</section>
<div class="navigation">
{{ knp_pagination_render(entities) }}
</div>
</div>
{% endblock %}
- you can overwrite controllers to manage results with user roles or domain (ie: different engine on admin or front)