chub / search-bundle
此包最新版本(dev-master)无可用许可证信息。
Symfony2 的站点搜索包
dev-master
2016-01-11 07:09 UTC
Requires
- php: >=5.5
- doctrine/common: ~2.0
- symfony/framework-bundle: 2.*
- symfony/yaml: 2.*
This package is not auto-updated.
Last update: 2024-09-20 20:54:20 UTC
README
搜索 Symfony2 扩展包。为您的网站提供易于使用的全局搜索服务。
安装
下载包
首先,使用以下常见方式之一下载包
使用 deps 文件
将以下行添加到您的 deps
文件中,并运行 php bin/vendors install
[SearchBundle]
git=https://github.com/ChubV/SearchBundle.git
target=bundles/ChubProduction/SearchBundle
使用 composer
注册命名空间
将以下命名空间条目添加到您的自动加载器中的 registerNamespaces
调用
<?php // app/autoload.php $loader->registerNamespaces(array( // ... 'ChubProduction\SearchBundle' => __DIR__.'/../vendor/bundles', // ... ));
如果使用 Composer 自动生成的自动加载文件,此步骤是不必要的
注册包
要开始使用包,请在 Kernel 中注册它
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new ChubProduction\SearchBundle\SearchBundle(), ); // ... }
使用搜索
创建搜索提供者,它必须实现 SearchProviderInterface
<?php // NewsSearchProvider.php class NewsSearchProvider implements SearchProviderInterface { private $m; /** * @param string $str * * @return \Doctrine\Common\Collections\ArrayCollection */ public function search($str) { // Perform search and return ArrayCollection of objects that implements SearchResultInterface // There is also default SearchResult class for this ChubProduction\SearchBundle\Service\SearchResult } /** * Return provider name * * @return string */ public function getName() { return 'news'; } /** * Return provider title * * @return string */ public function getTitle() { return 'News, events, etc '; }
在其他提供者中注册搜索提供者
# services.yml news.search.provider: class: ChubProduction\NewsBundle\Search\NewsSearchProvider tags: - { name: search.provider } content.search.provider: class: ... tags: - { name: search.provider } ....
使用搜索,获取包含来自您的搜索提供者的结果集的 ArrayCollection
<?php ... /** * @return array|\Symfony\Component\HttpFoundation\RedirectResponse * @Route("/search/{_locale}", name="search") * @Template() */ public function searchAction($_locale) { $r = $this->getRequest(); if ($r->query->has('q')) { $res = $this->get('search')->search($r->query->get('q')); // Here is our service return compact('res'); } return $this->redirect($this->generateUrl('main_page', compact('_locale'))); }