dniccum/nova-webhooks 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

dniccum/nova-webhooks

Composer 安装命令:

composer require dniccum/nova-webhooks

包简介

A tool for Laravel's Nova administrator panel that enables users to create webhooks that can be customized to fire on specified Eloquent model events (created, updated, etc). This allows applications to communicate with other applications and integrations (Zapier, If This Then That, etc).

README 文档

README

A Laravel Nova tool that allows users to create and manage webhooks based on Eloquent Model events.

Nova Webhooks

Latest Version on Packagist License Total Downloads

A tool for Laravel's Nova administrator panel that enables users to create webhooks that can be customized to fire on specified Eloquent model events (created, updated, etc). This allows applications to communicate with other applications and integrations (Zapier, If This Then That, etc).

This tool also includes a useful logging feature that will log any successful/failed webhooks to help with metrics and debugging.

Table of Contents

Installation

You can install the package via composer:

composer require dniccum/nova-webhooks

You will then need to publish the package's assets to your application:

php artisan vendor:publish --provider="Dniccum\NovaWebhooks\ToolServiceProvider"

Doing this action will add the following items:

  • migrations
  • configuration files (Note: there will be two)
  • translations

Migrations

Perform your database migrations via artisan command:

php artisan migrate

File Additions

There are two things that you will need to add to your application's NovaServiceProvider.php:

Addition 1 - Resource Trait

To inform Nova of the new resource that exists outside of the Nova directory within the application, we need to add a trait to the NovaServiceProvider class:

use Dniccum\NovaWebhooks\Nova\UsesWebhookResource;

...

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    // Add the following trait to your service provider
    use UsesWebhookResource;

}

Addition 2 - Tool Registration

Finally, you will need to register the tool within the NovaServiceProvider.php:

use Dniccum\NovaWebhooks\NovaWebhooks;

...

/**
 * Get the tools that should be listed in the Nova sidebar.
 *
 * @return array
 */
public function tools()
{
    return [
        // other tools
        new NovaWebhooks,
    ];
}

Configuration

Two different configuration files are published with this package; one for this package (nova-webhooks.php) and one for the webhook server (webhook-server.php) that this package utilizes.

This package relies on Spatie's webhook server package to dispatch each webhook request. Feel free to configure the server to your needs using the associated documentation.

Available Configuration Settings

Available configuration settings for this tool:

return [

    /**
     * Whether webhooks should be sent
     */
    'enabled' => env('NOVA_WEBHOOKS_ENABLED', true),

    /**
     * If logging should be enabled for each successful/failed request
     */
    'logging' => [
        'enabled' => env('NOVA_WEBHOOKS_LOGGING_ENABLED', true),
    ],

    /**
     * Enter the desired formatting for timestamps that are attached to logging.
     * See the official PHP documentation for more information: https://www.php.net/manual/en/datetime.format.php
     */
    'date_format' => 'Y-m-d @ G:i',

    /**
     * The Laravel Nova resource that manages your authenticated users.
     */
    'users' => [
        'resource' => App\Nova\User::class
    ]

];

Implementing the Tool

While this package is not necessarily "plug-and-play," it has been designed to only require minor amounts of code to get your webhooks up and running.

Nova Resource

The Webhook resource that is part of this tool will automatically be registered within Nova due to the trait that was added during setup process.

Model Traits

The main functionality of this tool is to listen to native Eloquent model events and passes a JSON-serialized payload of that model's attributes. A series traits are available for you to enable different actions and customize that hook's payload.

This package provides 4 different traits that you can add to each of your Eloquent models:

  • Dniccum\NovaWebhooks\Traits\CreatedWebhook
  • Dniccum\NovaWebhooks\Traits\UpdatedWebhook
  • Dniccum\NovaWebhooks\Traits\DeletedWebhook
  • Dniccum\NovaWebhooks\Traits\AllWebhooks

Each trait, with exception for the last (which is a shortcut to include all available traits), is associated with the event that it listens for; CreatedWebhook for the created Eloquent event and so on and so forth.

Customizing the Payload

Additionally, each trait provides a corresponding method to modify the payload that is sent with each webhook. See below for the name of the trait with the matching name of the method:

Trait Method
CreatedWebhook createdWebhookPayload
UpdatedWebhook updatedWebhookPayload
DeleteddWebhook deletedWebhookPayload

By default, this package will send a serialized array of the model that extends one of these traits. However you do have the ability to modify this behavior.

Custom Array

Return an associative array of valid key/value pairs based on the $model parameter.

protected static function createdWebhookPayload($model)
{
    return [
        'id' => $model->id,
        'name' => $model->first_name.' '.$model->last_name,
        'email' => $model->email,
        // And so on
    ];
}
JSON Resource

You also have the ability to return a Laravel-generated JSON resource and customize this object to your liking:

JSON Resource

class PageLikeResource extends \Illuminate\Http\Resources\Json\JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->first_name.' '.$this->last_name,
            'email' => $this->email,
        ];
    }
}

Eloquent Model

protected static function createdWebhookPayload($model)
{
    return new PageLikeResource($model);
}

Model Label

Each trait also exposes a method that will configure how the model displays within the Nova interface. By default Nova will show the namespaced string of the Model followed by the associated action; for instance App\Models\User:updated.

If you would like to change it to something else a little more user-friendly, or even use a translation key, you can modify it like so:

/**
 * The name of the model that will be applied to the webhook
 *
 * @return string
 */
public static function webhookLabel() : string
{
    return 'App Users';
}

Setting the label to App Users will show the following in the Nova action: App Users:updated.

Sending Webhooks to a Queue

When an Eloquent Model executes a webhook call, the webhook server package that helps power this tool will automatically send the webhook to the configured queue. In the case that you might be hydrating additional relationships and/or other logic while generating your payload, it might be more performant to send the execution of the webhook to the queue as well.

To do this, simply add the ShouldQueueWebhook trait to the top of your model, like so:

use Dniccum\NovaWebhooks\Jobs\DispatchWebhook;

class PageLike extends \Illuminate\Database\Eloquent\Model
{
    use ShouldQueueWebhook
    
    ...
}

This adds both a property and an additional method to your model:

/**
 * The job class that should be used to dispatch this model's webhook
 *
 * @var string
 */
public static $job = DispatchWebhook::class;

/**
 * @return bool
 */
public static function queueWebhook() : bool
{
    return true;
}

The property gives you the ability to override the Job class that is called when executing the webhook. For most cases this should not have to be changed, but it is available if necessary. Furthermore, you also have the ability to write custom logic to whether or not send the webhook to the queue at all. Again, most likely you shouldn't have to change this.

Using the Tool

Webhook Secret

The fields that the "baked-in" Webhook resource provides are relatively self-explanatory. All fields are required except for the hook's secret key. This will be auto-generated for you if you do not provide one your self. In most cases, your webhooks will not need a secret key for validation; both Zapier and IFTTT do not require any authorization.

In the circumstance that you are using a package/service like Spatie's webhook client, it is usually recommended (per their documentation) that you use a key authorize your application's webhook endpoints to prevent any unwanted requests. Again, you can either use the auto-generated hash that this package provides, or you can enter your own.

Authorization

Due to the fact the Webhook resource that is made available to the application via this package, you can provide user/role authorization using a conventional Eloquent Model policy. Refer to the Nova documentation for additional information.

Testing Action

Probably the most important part of any webhook is testing and validation that your webhook is working correctly and providing the necessary information to the prescribed endpoint. This package gives you the ability to do such an action based on the Eloquent events that you have selected for this webhook. Simply select the hook that you want to test and then utilize the Nova Actions toolbar or the inline button to launch the testing action, and then indicate which model and corresponding model event that you want to test.

Action Modal

Sample Data

When you want execute a test, this package will pull a random entry in the selected model's table in your database and use it as the subject for your webhook. If you don't have any records available yet, the action will throw an error instructing you to add the necessary records before you proceed.

Logging

Unless specifically configured (as seen above), this tool will log successful and failed webhook operations. Successful events are simply stored for analytics purposes and are then displayed with some simple accompanying charts.

Resource Analytics

If the desired endpoint that your webhooks are pointed throws an error, this tool will capture the response and log it accordingly. You can then view the associated error message within the Webhook that was created.

Testing and Development

To perform the necessary PHP Unit tests using the Orchestra Workbench, clone the repository, install the necessary dependencies with composer install and run the PHP Unit testing suite:

./vendor/bin/phpunit

dniccum/nova-webhooks 适用场景与选型建议

dniccum/nova-webhooks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.87k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2022 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「server」 「nova」 「laravel」 「webhooks」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dniccum/nova-webhooks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dniccum/nova-webhooks 我们能提供哪些服务?
定制开发 / 二次开发

基于 dniccum/nova-webhooks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.87k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-17