distilleries/contentful 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

distilleries/contentful

Composer 安装命令:

composer require distilleries/contentful

包简介

Package to use Contentful in offline mode.

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage Total Downloads Latest Stable Version License

Distilleries / Laravel-Contentful-Utilities

Laravel-Contentful-Utilities is a Laravel 5.6 - 5.8 / Lumen package 5.6 - 5.8 package to use contentful in offline mode with and without preview. Contentful is a headless CMS in cloud you can have more information on their website https://www.contentful.com

Features

  • Model generator from contentful

  • Migration generator from contentful

  • Synchronization from contentful to database

Installation

Composer

Install the [composer package] by running the following command:

composer require distilleries/contentful

Models and Mapper

When we synchronize all the data on database the mapper link to the model are call. This mapper car provide the extract of field you would like one the database. For example you want to externalize the title and the slug on the database you have to change the migration generated and the mapper.

    class TerritoryMapper extends ContentfulMapper
    {
        /**
         * {@inheritdoc}
         */
        protected function map(array $entry, string $locale): array
        {
            $payload = $this->mapPayload($entry, $locale);
    
            return [
                'slug' => isset($payload['slug']) ? Caster::string($payload['slug']) : '',
                'title' => isset($payload['title']) ? Caster::string($payload['title']) : '',
            ];
        }
    }
    class Territory extends ContentfulModel
    {
        /**
         * {@inheritdoc}
         */
        protected $table = 'territories';
    
        /**
         * {@inheritdoc}
         */
        protected $fillable = [
            'slug',
            'title',
        ];
    
        /**
         * Picture attribute accessor.
         *
         * @return \Distilleries\Contentful\Models\Asset|null
         */
        public function getPictureAttribute(): ?Asset
        {
            return isset($this->payload['picture']) ? $this->contentfulAsset($this->payload['picture']) : null;
        }
    }

All the model generated have a getters for all the payload fields. If you want to externalize the field on database.

Command-line tools

To make model and mapper from contentful

  • php artisan contentful:generate-models

ℹ️ Models are generated on app_path('Models'); and the mappers are generated on app_path('Models/Mappers');

To make migration from contentful model

  • php artisan contentful:generate-migrations

To launch the synchronisation you can use this command line

  • php artisan contentful:sync-data {--preview}

  • php artisan contentful:sync-flatten {--preview}

ℹ️ --preview is optional and use if you want to flatten the preview database.

Command Explain
sync-data Get all the entries from contentful and put in the flatten database
sync-flatten Get all the entries from data table to explode on all the other types

Webhook

To flatten the preview or the regular database you need to set the webhook on Contentful

Create a controller and use the trait:

use \Distilleries\Contentful\Http\Controllers\WebhookTrait;

Make the route callable in post:

    $router->post('/webhook/live', 'WebhookController@live');
    $router->post('/webhook/preview', 'WebhookController@preview');
  • Live method is called to save on live mode

  • Preview method is called to save the preview data

To display the site with preview database you have to use UsePreview middleware.

    $router->group(['prefix' => 'preview', 'middleware' => 'use_preview'], function () use ($router) {
        //
    });

Add your middleware:

    'use_preview' => Distilleries\Contentful\Http\Middleware\UsePreview::class,

统计信息

  • 总下载量: 710
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固