takethelead/laravel-storyblok

2.1.0 2023-10-02 15:43 UTC

This package is auto-updated.

Last update: 2023-10-02 15:49:44 UTC


README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Laravel storyblok 是一个围绕官方 Storyblok php 客户端进行包装并提供一些额外 Laravel 优点的包装器。

安装

您可以通过 composer 安装此软件包

composer require takethelead/laravel-storyblok

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="TakeTheLead\LaravelStoryblok\LaravelStoryblokServiceProvider" --tag="config"

这是已发布配置文件的内容

<?php

return [

    /**
     * The api key used to authenticated with the Storyblok api.
     */
    'api_key' => env('STORYBLOK_API_KEY'),

    /**
     * Cache Storyblok api responses.
     */
    'enable_local_cache' => env('STORYBLOK_ENABLE_LOCAL_CACHE', true),

    /**
     * Cache filename.
     */
    'cache_version_file_name' => 'storyblok-content-version.json',

    /**
     * Cache location on the selected disk
     */
    'cache_path' => 'storyblok/cache',

    /**
     * Storage disk
     */
    'disk' => 'local',

    /**
     * Enable edit mode.
     */
    'enable_edit_mode' => env('STORYBLOK_ENABLE_EDIT_MODE', false),

    /**
     * The webhook secret, you can leave this empty when you are not using a secret.
     */
    'webhook_secret' => env('STORYBLOK_WEBHOOK_SECRET', ''),

    /**
     * The middleware that should be applied to the webhook route.
     */
    'route_middleware' => [
        \TakeTheLead\LaravelStoryblok\Http\Middleware\VerifyStoryblokWebhookSignature::class,
    ],

    /**
     * The actions that should be execute whenever the webhook gets called.
     *
     * All actions should implement the \TakeTheLead\LaravelStoryblok\Actions\ActionInterface
     */
    'webhook_actions' => [
        \TakeTheLead\LaravelStoryblok\Actions\ClearCacheAction::class,
    ],
];

使用方法

基本类

此软件包提供对默认 Storyblok 客户端类的包装,并将其注册在 Laravel 服务容器中。

use Storyblok;

Storyblok::getStoryBySlug("story-slug");
Stroyblok::getStoryByUuid("story-uuid");

需要其他功能?

如果您需要更多功能,您可以通过以下方式获取底层 Storyblok API 的实例:

use Storyblok;

$api = Storyblok::getApi();

处理 webhook

此软件包附带处理 webhook 的预配置设置。只需将以下路由添加到您的路由文件中即可。

Route::storyblok();

请确保将此路由添加到 except 数组中的 VerifyCsrfToken 中间件,以防止令牌不匹配。

验证 webhook 签名

如果您在 Storyblok 中配置了 webhook 秘密,请确保将其添加到您的 env 文件中的环境变量。默认情况下,一个中间件被应用到您的路由以验证签名。然而,您可以在配置文件的 route_middleware 设置中自由添加您自己的实现(或添加额外的中间件)。

STORYBLOK_WEBHOOK_SECRET=your-secret

每当 webhook 被调用时执行操作

在配置文件中,您可以指定在调用 webhook 时应该执行哪些操作。设置键为 webhook_actions。当添加您自己的操作类时,请确保它们实现了 \TakeTheLead\LaravelStoryblok\Actions\ActionInterface。否则,您的操作将不会执行。

示例

<?php

use \TakeTheLead\LaravelStoryblok\Actions\ActionInterface;

class YourAction implements ActionInterface
{
    public function __construct()
    {
        // you can inject any class registerd within the IOC container
    }

    public function execute(): void
    {
        // Perform your action
    }
}

ClearStoryblokCacheCommand

此软件包提供了一种指定内容缓存版本到 Storyblok 的方法,您可以通过执行以下 artisan 命令来清除当前缓存版本。

注意:建议您有一个从 Storyblok 处理部署和内容更改的工作流程。这可以通过将命令添加到您的部署脚本以及配置 Storyblok 中的 webhook 来监听内容更改来实现。

php artisan storyblok:clear-cache

富文本解析器

该软件包自动要求并安装 Storyblok richtecht 解析器软件包以解析故事内容格式。更多信息可以在官方包中找到。

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

请查看 我们的安全策略,了解如何报告安全漏洞。

鸣谢

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件