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
-
Model generator from contentful
-
Migration generator from contentful
-
Synchronization from contentful to database
Install the [composer package] by running the following command:
composer require distilleries/contentful
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.
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 |
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,