将搜索框添加到在Sylius上构建的网站的主页

时间:2017-06-26 08:07:19

标签: symfony search composer-php e-commerce sylius

有人可以解释我如何在Sylius建立的网站主页上添加搜索框吗?

我尝试过使用SyliusSearchBundle,但这似乎非常缺乏数据,而且示例配置设置会抛出错误。

我很感激任何帮助。

2 个答案:

答案 0 :(得分:2)

<强>声明 在我写作的时候,下面的步骤工作。 SyliusElasticSearchBundle插件处于繁重的开发状态,现在有些东西崩溃,但肯定会被修复。

我在这里创建了一个演示存储库:https://github.com/mheki/sylius-search-demo

您需要运行ElasticSearch和SyliusElasticSearchBundle插件。

按照自述文件中的安装说明进行操作:https://github.com/Lakion/SyliusElasticSearchBundle

请记住安装sylius dev-master(循环依赖...)

导入bundle的配置,路由,在AppKernel中启用它。

目前我正在编写需要filter_sets配置的捆绑包,否则崩溃

首先,按产品名称进行简单搜索:

lakion_sylius_elastic_search:
    filter_sets:
        default:
            filters:
                name: 
                    type: string

用弹性索引填充弹性索引:

bin/console fos:elastic:pop

覆盖lakion_elastic_search_shop_product_index的原始路由 - 使用filter_set:作为您的频道代码。

lakion_elastic_search_shop_product_index:
    path: /products
    methods: [GET]
    defaults:
         _controller: lakion_sylius_elastic_search.controller.search:filterAction
         _sylius:
            template: "@LakionSyliusElasticSearch/Product/index.html.twig"
            resource_class: "%sylius.model.product.class%"
            filter_set: default
    requirements:
        slug: .+

原始产品索引页面失败了,我不得不删除

{{ form_row(form.search) }}

从中。所以将@LakionSyliusElasticSearch/Product/index.html.twig复制到Resources目录中: Resources\LakionSyliusElasticSearchBundle\views\Product\index.html.twig并做出了改变。

现在最后一件事就是创建一个表单,例如从SyliusShopBundle复制文件_security.html.twig。添加如下内容:

<div class="item">
        <form action="{{ path('lakion_elastic_search_shop_product_index') }}" method="get">
            <div class="ui icon input">
                <input type="text" placeholder="{{ 'sylius.ui.search'|trans }}..." name="filter_set[name]" />
                <button type="submit" class="ui button mini">
                    <i class="search icon"></i>
                </button>
            </div>
        </form>
</div>

在这里我们去:)

search on demo shop

答案 1 :(得分:0)

或者,如果您不想使用SyliusElasticSearchBundle插件,则可以自己实现一个简单的搜索(在产品名称,描述和类别名称中进行搜索):

创建经典的html搜索表单,例如:

<form role="search" method="get" action="{{path('app_search_results') }}">
    <input type="search" id="site-search" name="q"
           placeholder="{{ 'walrus.search.input.placeholder'|trans }}"
           aria-label="{{ 'walrus.search.aria.label'|trans }}">
    <button>{{ 'walrus.search.aria.button'|trans }}</button>
</form>

为路线“ app_search_results”创建控制器操作,以获取您的搜索表单发送的搜索请求:

<?php

namespace AppBundle\Controller;

use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use \Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class SearchController extends Controller
{

/**
 * @var LocaleContextInterface
 */
private $locale;
/**
 * @var ChannelContextInterface
 */
private $channelContext;
/**
 * @var RepositoryInterface
 */
private $productRepository;

public function __construct(
    LocaleContextInterface $locale,
    ChannelContextInterface $channelContext,
    RepositoryInterface $productRepository
)
{
    $this->locale = $locale;
    $this->channelContext = $channelContext;
    $this->productRepository = $productRepository;
}

/**
 * @Route("/search", name="app_search_results")
 */
public function searchAction(Request $request) : Response
{

    $searchTerm = $request->query->get('q');

    $channel = $this->channelContext->getChannel();
    $localeCode = $this->locale->getLocaleCode();

    $products = $this->productRepository->findByTerm($channel, $localeCode, $searchTerm);


    return $this->render(
        '@App/search/searchListProducts.html.twig',
        [
            'products' => $products
        ]
    );
}

}

不要忘记手动连接通过构造函数注入的依赖项。

重写产品存储库以实现findByTerm()方法:

<?php


namespace AppBundle\Repository;

use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as 
BaseProductRepository;
use Sylius\Component\Core\Model\ChannelInterface;


class ProductRepository extends BaseProductRepository
{

public function findByTerm(ChannelInterface $channel, string $locale, $searchTerm): array
{
    $qb = $this->createQueryBuilder('p')
                ->addSelect('translation')
                // get the translated product for the product regarding the current locale
                ->innerJoin('p.translations', 'translation', 'WITH', 'translation.locale = :locale')
                ->orWhere('translation.name LIKE :searchTerm')
                ->orWhere('translation.description LIKE :searchTerm')
                // get the taxons of the product
                ->innerJoin('p.productTaxons', 'productTaxon')
                ->innerJoin('productTaxon.taxon', 'taxon')
                // get the translated taxon
                ->innerJoin('taxon.translations', 'taxonTranslation', 'WITH', 'taxonTranslation.locale = :locale')
                ->orWhere('taxonTranslation.name LIKE :searchTerm')

                ->andWhere(':channel MEMBER OF p.channels')
                ->andWhere('p.enabled = true')
                ->setParameter('searchTerm', '%'.$searchTerm.'%')
                ->setParameter('locale', $locale)
                ->setParameter('channel', $channel)
                ->getQuery();

    return $qb->getResult();

}

}

您现在只需要创建呈现产品列表的页面(@ App / search / searchListProducts.html.twig)。并告诉Sylius使用您自定义的HomepageController而不是原始的HomepageController。您的产品存储库也是如此。这可以在您的应用文件夹的services.yml文件中完成:

services:
    sylius.controller.shop.homepage:
        public: true
        class: AppBundle\Controller\Shop\HomepageController
        arguments:
            - '@templating'
            - '@sylius.context.locale'
            - '@sylius.repository.taxon'

sylius_product:
    resources:
        product:
            classes:
                repository: AppBundle\Repository\ProductRepository