GithubHelp home page GithubHelp logo

bbs-smuller / laravel-contentful-utilities Goto Github PK

View Code? Open in Web Editor NEW

This project forked from distilleries/laravel-contentful-utilities

0.0 1.0 0.0 277 KB

Laravel Contentful to use it in offline

License: MIT License

PHP 100.00%

laravel-contentful-utilities's Introduction

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,

laravel-contentful-utilities's People

Contributors

bbs-smuller avatar mfrancois avatar mfrancoisbbs avatar mikaelpopowicz avatar waudouinbbs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.