jurihub/laravel-webhooks

Laravel Webhooks 处理包

v0.1.5 2021-04-06 18:11 UTC

This package is not auto-updated.

Last update: 2024-09-18 10:04:27 UTC


README

Laravel Webhooks 处理包

安装指南

通过 Composer 包含页面

composer require jurihub/laravel-webhooks

将 Webhooks 服务提供者添加到您的 config/app.php 文件中的 providers 数组中

Jurihub\LaravelWebhooks\WebhooksServiceProvider::class

要使用 Facade 而不是直接注入类,将以下内容添加到同一文件的 aliases 数组中

'Webhooks' => Jurihub\LaravelWebhooks\WebhooksFacade::class

发布配置文件

php artisan vendor:publish --provider="Jurihub\LaravelWebhooks\WebhooksServiceProvider" --tag="config"

您当然会想要添加一些端点,在 config/webhooks.php 文件的 targets 数组中列出它们

运行迁移(由 ServiceProvider 自动提供),这将创建 2 个新表:webhookswebhook_tries

php artisan migrate

第一次尝试将自动发送,但如果您想自动化重试,请将以下调度添加到您的 app/Console/Kernel.php 文件中

$schedule->call(function () {
    \Jurihub\LaravelWebhooks\Http\Controllers\Webhooks\SenderController::retry();
})->everyMinute();

要处理传入的 Webhooks,创建一个新的控制器,例如 App\Http\Controllers\Webhooks\ReceiverController.php

namespace App\Http\Controllers\Webhooks;

use Symfony\Component\HttpFoundation\Response;
use Jurihub\LaravelWebhooks\Http\Controllers\Webhooks\ReceiverController as BaseController;

class ReceiverController extends BaseController
{
    public function handleUserUpdated($data)
    {
        // handling $data here for type 'user.updated'
    }
}

并将路由添加到您的 routes/api.php 文件中,以接收传入的 Webhooks。您可能想要根据 config/app.php 文件中列出的 targets 自定义端点。

Route::post('webhook', 'Webhooks\ReceiverController@handleWebhook');

激活 Webhooks 的发送队列

php artisan queue:work database --queue=webhook --tries=3