syzygypl/kunstmaan-feed-bundle

3.2.4 2018-08-14 12:46 UTC

README

步骤 1: 下载组件

打开命令行控制台,进入您的项目目录,然后执行以下命令以下载此组件的最新稳定版本

$ composer require syzygypl/kunstmaan-feed-bundle

此命令需要您全局安装Composer,如Composer文档中的安装章节中所述。

步骤 2: 启用组件

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

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new  SZG\KunstmaanFeedBundle\KunstmaanFeedBundle(),
        );

        // ...
    }

    // ...
}

用法

article下面表示一个已索引的类型。它可以在页面配置中定义为search_type。

kunstmaan_node:
    pages:
        'AppBundle\Entity\Pages\ArticlePage':
            ...
            indexable: true
            search_type: article

TWIG

最近100篇文章
{# Short notation, use defaults #}
{% set articles = get_article_feed() %}

{# Simple notation #}
{% set articles = get_recent_article_feed({ limit: 100 }) %}

{# full notation #}
{% set articles = get_feed_items('article', { limit: 100, feed: 'recent' }) %}
随机产品
{% set random_product = get_random_product_feed({ limit: 1 }) %}

PHP

最近100篇文章
<?php
// ...

$articles = $this->get('szg_feed.elastic_search_items_provider')->getFeedItems('article', [
    'feed' => 'recent',
    'limit' => 100
]);
随机产品
<?php
// ...

$articles = $this->get('szg_feed.elastic_search_items_provider')->getFeedItems('product', [
    'feed' => 'random',
    'limit' => 1
]);

选项参考

定义自定义Feed类型

排序单个字段自定义Feed

在config.yml中配置自定义Feed

kunstmaan_feed:
    custom: 
        title : 'desc'
            

高级自定义Feed

1. 实现FeedElasticSearchInterface

实现SZG\KunstmaanFeedBundle\Feed\ElasticSearch\Interfaces\FeedElasticSearchInterface

<?php
// ...
class TitleDesc extends FeedElasticSearchInterface
{

   /**
    * @param QueryDefinition $queryDefinition
    */
   public function modifyQuery(QueryDefinition $queryDefinition)
   {
        $queryDefinition->getQuery()->addSort(['title' => ['order' => 'desc']]);
   }

}

2. 注册自定义Feed服务

szg_feed.feed.title:
    class: AppBundle\Feed\TitleDesc
    tags:
      - { name: szg_feed.feed, alias: title }

使用自定义Feed

{% set articlesByTitle = get_title_article_feed() %}