distilleries / contentful
用于离线模式下使用Contentful的包。
Requires
- php: >=7.1.3
- ext-json: *
- contentful/contentful: ^4.1
- erusev/parsedown: ^1.7
- guzzlehttp/guzzle: ^6.3
- illuminate/cache: ~5.6|~5.7|~5.8|^6.0
- illuminate/database: ~5.6|~5.7|~5.8|^6.0
- illuminate/support: ~5.6|~5.7|~5.8|^6.0
- jenssegers/agent: ^2.6
Requires (Dev)
- mockery/mockery: ^1.1|^1.2.3
- orchestra/database: ~3.6|~3.7|~3.8|^4.0
- orchestra/testbench: ~3.6|~3.7|~3.8|^4.0
- phpunit/phpunit: ~7.0|^8.3
README
Distilleries / Laravel-Contentful-Utilities
Laravel-Contentful-Utilities是一个用于在离线模式下(带预览和不带预览)使用contentful的Laravel 5.6 - 5.8 / Lumen 5.6 - 5.8包。Contentful是一个云端的头尾分离内容管理系统,您可以在其网站上了解更多信息:https://www.contentful.com
特性
-
从contentful生成模型
-
从contentful生成迁移
-
从contentful到数据库的同步
安装
Composer
通过运行以下命令安装[composer包]
composer require distilleries/contentful
模型和映射器
当我们同步数据库上的所有数据时,映射器将链接到模型。这个映射器可以提供数据库上您想要提取的字段。例如,如果您想将标题和slug外部化到数据库中,您必须更改生成的迁移和映射器。
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; } }
所有生成的模型都有所有有效负载字段的getter。如果您想将字段外部化到数据库中。
命令行工具
从contentful生成模型和映射器
php artisan contentful:generate-models
ℹ️ 模型生成在app_path('Models'),映射器生成在app_path('Models/Mappers');
从contentful模型生成迁移
php artisan contentful:generate-migrations
要启动同步,可以使用以下命令行
-
php artisan contentful:sync-data {--preview}
-
php artisan contentful:sync-flatten {--preview}
ℹ️ --preview是可选的,如果您想扁平化预览数据库,则使用。
Webhook
要扁平化预览或常规数据库,您需要在Contentful上设置webhook。
创建控制器并使用特性
use \Distilleries\Contentful\Http\Controllers\WebhookTrait;
将路由设置为可以通过POST调用
$router->post('/webhook/live', 'WebhookController@live'); $router->post('/webhook/preview', 'WebhookController@preview');
-
Live方法在实时模式下被调用以保存
-
Preview方法在预览模式下被调用以保存预览数据
要显示使用预览数据库的网站,您必须使用UsePreview中间件。
$router->group(['prefix' => 'preview', 'middleware' => 'use_preview'], function () use ($router) { // });
添加您的中间件
'use_preview' => Distilleries\Contentful\Http\Middleware\UsePreview::class,