定制 viezel/webhooks 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

viezel/webhooks

Composer 安装命令:

composer require viezel/webhooks

包简介

Webhooks for Laravel

关键字:

README 文档

README

Latest Version on Packagist GitHub Tests Action Status

Simple and powerful implementation of Webhooks.

Installation

You can install the package via composer:

composer require viezel/webhooks

You can publish and run the migrations with:

php artisan vendor:publish --provider="Viezel\Webhooks\WebhooksServiceProvider" --tag="migrations"
php artisan migrate

Add routes to your application. Below is a typical route configuration with auth, api prefix and naming.

Route::middleware('auth:api')->prefix('api')->as('webhooks.api.')->group(function() {
    Route::get('hooks', Viezel\Webhooks\Controllers\API\ListWebhooks::class)->name('list');
    Route::get('hooks/events', Viezel\Webhooks\Controllers\API\ListWebhookEvents::class)->name('events');
    Route::post('hooks', Viezel\Webhooks\Controllers\API\CreateWebhook::class)->name('create');
    Route::delete('hooks/{id}', Viezel\Webhooks\Controllers\API\DeleteWebhook::class)->name('delete');
});

Usage

First, register Events in your application that should be exposed as Webhooks. To do so, your Events should implement the ShouldDeliverWebhooks interface.

The interface has two methods, getWebhookName for giving the webhook a unique name, and getWebhookPayload to define the data send with the webhook.

The following example shows how a Post Updated Event and its implementation:

use App\Models\Post;
use Viezel\Webhooks\Contracts\ShouldDeliverWebhooks;

class PostUpdatedEvent implements ShouldDeliverWebhooks
{
    public function __construct(Post $post)
    {
        $this->post = $post;
    }

    public function getWebhookName(): string
    {
        return 'post:updated';
    }

    public function getWebhookPayload(): array
    {
        return [
            'post' => $this->post->toArray(),
            'extra' => [
                'foo' => 'bar'
            ]       
        ];
    }
}

Next you need to register all your events with the WebhookRegistry. This is typically done in the boot method of a ServiceProvider.

public function boot()
{
    WebhookRegistry::listen(PostUpdatedEvent::class);
}

To check everything works as expected, go visit the webhooks events route. The default route is: /api/hooks/events. It depends how you register the webhook routes.

List available webhooks events

GET https://myapp.test/api/hooks/events

List registered webhooks

GET https://myapp.test/api/hooks

Register a webhook

POST https://myapp.test/api/hooks

{
    "events": [
        "post:updated"
    ],
    "url": "https://another-app.com/some/callback/route"
}

Delete a webhook

DELETE https://myapp.test/api/hooks/{id}

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固