bestit/contentful-bundle

一个简单的contentful包,提供简单的设置、内容模型和简单的API,将contentful内容作为数组提供。

1.1.2 2019-07-12 12:12 UTC

README

装饰客户端,并将contentful模型作为数组提供,正如在contentful的内容类型中定义的那样。额外的甜点是模板辅助器,便于访问。

安装

步骤 1:下载包

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

$ composer require bestit/contentful-bundle

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

步骤 2:启用包

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

<?php
// app/AppKernel.php

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

            new BestIt\ContentfulBundle\BestItContentfulBundle(),
        );

        // ...
    }

    // ...
}

步骤 3:配置参考

best_it_contentful:

    # This field indicates which controller should be used for the routing.
    controller_field:     controller

    # Which field is used to mark the url of the contentful entry?
    routing_field:        slug

    # This content types have a routable page in the project.
    routable_types:       []
    caching:              # Required
        content:

            # Please provider your service id for caching contentful contents.
            service_id:           ~ # Required

            # Please provide the ttl for your content cache in seconds. 0 means forever.
            cache_time:           0
        routing:

            # Please provider your service id for caching contentful routings.
            service_id:           ~ # Required

            # Please provide the ttl for your routing cache in seconds. 0 means forever.
            cache_time:           0

            # If the requested url contains this query parameter, the routing cache will be ignored.
            parameter_against_routing_cache:  ignore-contentful-routing-cache

        # Should the whole contentful cache be cleared every time on an entry reset request?
        complete_clear_on_webhook:  false

        # Which cache ids should be resetted everytime?
        collection_consumer:  []

    # Add the content types mainly documented under: <https://www.contentful.com/developers/docs/references/content-management-api/#/reference/content-types>
    content_types:

        # Prototype
        id:
            description:          ~ # Required
            displayField:         ~ # Required
            name:                 ~ # Required

            # Give the logical controller name for the routing, like document under <https://symfony.com.cn/doc/current/routing.html#controller-string-syntax>
            controller:           ~
            fields:               # Required

                # Prototype
                id:
                    linkType:             ~ # One of "Asset"; "Entry"
                    name:                 ~ # Required
                    omitted:              false
                    required:             false
                    items:
                        type:                 ~ # One of "Link"; "Symbol"
                        linkType:             ~ # One of "Asset"; "Entry"
                        validations:
                            linkContentType:      []
                    type:                 ~ # One of "Array"; "Boolean"; "Date"; "Integer"; "Location"; "Link"; "Number"; "Object"; "Symbol"; "Text", Required

                    # Shortcut to handle the editor interface for this field, documentation can be found here: <https://www.contentful.com/developers/docs/references/content-management-api/#/reference/editor-interface>
                    control:              # Required
                        id:                   ~ # One of "assetLinkEditor"; "assetLinksEditor"; "assetGalleryEditor"; "boolean"; "datePicker"; "entryLinkEditor"; "entryLinksEditor"; "entryCardEditor"; "entryCardsEditor"; "numberEditor"; "rating"; "locationEditor"; "objectEditor"; "urlEditor"; "slugEditor"; "ooyalaEditor"; "kalturaEditor"; "kalturaMultiVideoEditor"; "listInput"; "checkbox"; "tagEditor"; "multipleLine"; "markdown"; "singleLine"; "dropdown"; "radio", Required
                        settings:             # Required
                            ampm:                 ~
                            falseLabel:           ~
                            format:               ~
                            helpText:             ~ # Required
                            stars:                ~
                            trueLabel:            ~
                    validations:
                        size:
                            max:                  ~
                            min:                  ~
                        in:                   []
                        linkContentType:      []
                        unique:               ~

步骤 4:启用缓存重置webhook

此包会无限期缓存每个contentful条目,但...

您可以使用contentful webhook来重置您的缓存条目。

best_it_contentful:
    prefix:   /bestit/contentful
    resource: "@BestItContentfulBundle/Controller"
    type:     annotation

只需将重置控制器添加到您的路由(我们建议使用http auth密码保护)并在contentful webhook配置中输入此URL,您就可以开始了。

最简单的缓存重置是直接根据id或id数组(用作缓存标签)进行重置。

如果您有与条目id不直接匹配但与您自己的自定义缓存id匹配的缓存条目,则需要填写配置值caching.collection_consumer以重置它们,以便在重置另一个缓存时随时重置。

使用

内容模型创建器

$php bin/console contentful:create-types`

此命令会复制您在contentful项目中配置的内容类型。

客户端装饰器

<?php

/** @var \BestIt\ContentfulBundle\Service\Delivery\ClientDecorator $clientDecorator */
$clientDecorator = $this->getClient();

$contentType = 'example-type';
$limit = 5;
$where = ['fields.example-slug' => 'example-com'];

if (is_scalar($where)) {
    $entry = $clientDecorator->getEntry($id = $where);
} else {
    $entries = $clientDecorator->getEntries(
        function (\Contentful\Delivery\Query $query) use ($contentType, $limit, $where) {
            $query->setContentType($contentType);

            if ($limit) {
                $query->setLimit($limit);
            }

            array_walk($where, function ($value, $key) use ($query) {
                $query->where($key, $value);
            });

            return $query;
        },
        $cacheId = sha1(__METHOD__ . ':' . $contentType . ':' . serialize($where))
    );
}

路由器

如果您想通过URL直接将contentful元素匹配到应用程序的控制器操作,我们提供路由器。只需将我们的Slug-Matcher注册到您的CMF-Routing-Chain

您可以选择使用以下服务声明进行缓存Slug-Matcher

services: 
 app.router.contentful:
     class: BestIt\ContentfulBundle\Routing\CachingContentfulSlugMatcher
     public: false
     arguments:
         - '@best_it_contentful.cache.pool.routing'
         - '@contentful.delivery.contentful_client'
         - '%best_it_contentful.controller_field%'
         - '%best_it_contentful.routing_field%'
         - '@best_it_contentful.routing.route_collection_response_parser'
         - '@best_it_contentful.delivery.response_parser'
         - '%best_it_contentful.cache.parameter_against_routing_cache%'
     calls:
         - [setRoutableTypes, ['%best_it_contentful.routable_types%']]
     tags:
         - { name: router, priority: 0 }  # Adjusted to not override the manual routing done by Symfony

或非缓存Slug-Matcher

services: 
    app.router.contentful:
        class: BestIt\ContentfulBundle\Routing\ContentfulSlugMatcher
        public: false
        arguments:
            - '@best_it_contentful.delivery.client'
            - '%best_it_contentful.controller_field%'
            - '%best_it_contentful.routing_field%'
            - '@best_it_contentful.routing.route_collection_response_parser'
        calls:
            - [setRoutableTypes, ['%best_it_contentful.routable_types%']]
        tags:
            - { name: router, priority: 0 }  # Adjusted to not override the manual routing done by Symfony

请注意,CachingContentfulSlugMatcher将数据作为数组传递到您的控制器,而非缓存ContentfulSlugMatcher传递contentful条目

视图辅助器

您可以使用以下twig辅助器轻松访问

  1. get_contentful:加载匹配您查询的条目/条目并返回它。如果您加载条目,您需要保存结果数组。
  2. parseMarkdown:此视图辅助器使用Parsedown并将contentful内容转换为html。

进一步步骤

  • 更多文档
  • 更好的单元测试
  • behat测试