2017 © Pedro PelĂĄez
 

library contentful

Package to use Contentful in offline mode.

image

distilleries/contentful

Package to use Contentful in offline mode.

  • Friday, July 27, 2018
  • by Kezho
  • Repository
  • 2 Watchers
  • 0 Stars
  • 201 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Build Status Scrutinizer Code Quality Code Coverage Total Downloads Latest Stable Version License, (*1)

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, (*2)

Features

  • Model generator from contentful, (*3)

  • Migration generator from contentful, (*4)

  • Synchronization from contentful to database, (*5)

Installation

Composer

Install the [composer package] by running the following command:, (*6)

composer require distilleries/contentful, (*7)

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., (*8)

    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., (*9)

Command-line tools

To make model and mapper from contentful, (*10)

  • php artisan contentful:generate-models

:information_source: Models are generated on app_path('Models'); and the mappers are generated on app_path('Models/Mappers');, (*11)

To make migration from contentful model, (*12)

  • php artisan contentful:generate-migrations

To launch the synchronisation you can use this command line, (*13)

  • php artisan contentful:sync-data {--preview}, (*14)

  • php artisan contentful:sync-flatten {--preview}, (*15)

:information_source: --preview is optional and use if you want to flatten the preview database., (*16)

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, (*17)

Create a controller and use the trait:, (*18)

use \Distilleries\Contentful\Http\Controllers\WebhookTrait;, (*19)

Make the route callable in post:, (*20)

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

  • Preview method is called to save the preview data, (*22)

To display the site with preview database you have to use UsePreview middleware., (*23)

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

Add your middleware:, (*24)

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

The Versions

27/07 2018
02/07 2018

dev-feature/sync-switch

dev-feature/sync-switch

Package to use Contentful in offline mode.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois
by Sébastien Muller

21/06 2018

dev-feature/testing

dev-feature/testing

Package to use Contentful in offline mode.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois
by Sébastien Muller

18/06 2018

dev-develop

dev-develop

Package to use contentful in offline mode.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois