承接 mamadali/yii2-webhook 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

mamadali/yii2-webhook

最新稳定版本:v1.51

Composer 安装命令:

composer require mamadali/yii2-webhook

包简介

Yii2 send create and changes to webhook

README 文档

README

Helps you to send models changes to a webhook url

Latest Stable Version Total Downloads

Installation

The preferred way to install this extension is through composer.

Either run

composer require --prefer-dist mamadali/yii2-webhook "*"

or add

"mamadali/yii2-webhook": "*"

to the require section of your composer.json file.

then run migrations

php yii migrate/up --migrationPath=@vendor/mamadali/yii2-webhook/migrations

#Basic usage first add to config.php or if use advanced project add to common/config/main.php

    'modules' => [
        ...
        'webhook' => [
            'class' => 'mamadali\webhook\Module',
            'url' => 'https://example.com/webhook',
        ],
        ...
    ];

To send model change to webhook you need to add to your model

public function behaviors()
{
    return [
		[
		    'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
		],
	];
}

When any changes on your model, will be send data to webhook like this:

{
    "model_name": "ExampleModel",
    "action": "insert", // or update or delete
    "model_id": 4, // id of model
    "data": { // all data of your model
        "id": 4,
        "title": "Example title",
        "status": 1,
        "created_at": 1633153382,
        "updated_at": 1645517769
    }
}

#Advanced usage

You can add authentication to your webhook url, like this:

for use basic auth:

    'modules' => [
        ...
        'webhook' => [
            'class' => 'mamadali\webhook\Module',
            'url' => 'https://example.com/webhook',
            'authToken' => base64_encode("$username:$password"), // change username and password
        ],
        ...
    ];

for use bearer auth:

    'modules' => [
        ...
        'webhook' => [
            'class' => 'mamadali\webhook\Module',
            'url' => 'https://example.com/webhook',
            'authMethod' => 'Bearer',
            'authToken' => $token, // your token here
        ],
        ...
    ];

###you can change url and auth for send webhook in any model, like this:

public function behaviors()
{
	return [
		[
			'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'url' => 'https://example.com/webhook/example-model',
		],
	];
}

you can send data to webhook only on specific scenarios or except scenarios, like this:

public function behaviors()
{
	return [
		[
			'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'scenarios' => ['insert', 'update'], // send data only on these scenarios
			'except' => ['delete'], // Send data except in these scenarios
		],
	];
}

you can customize attributes to send in webhook, like this:

public function behaviors()
{
	return [
		[
		    'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'attributes' => [
				'title' => function (self $model) {
					return $model->getFullTitle();
				},
				'email',
				'created_at' => function (self $model) {
                    return date('Y-m-d H:i:s', $model->created_at);
                },
			],
		],
	];
}

you can set excepted attributes, if set, will be send all attributes except the ones in this array

public function behaviors()
{
	return [
		[
		    'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'exceptAttributes' => [
				'status',
				'password'
			],
		],
	];
}

you can use 'when' property to send data only on when this function return true, like this:

public function behaviors()
{
	return [
		[
		    'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'when' => function(self $model) {
                            return $model->status == 1;
			},
		],
	];
}

####you can send webhook with queue to use queue you need first configure queue from Yii2 Queue Document

then you can use queue in your model, like this:

public function behaviors()
{
	return [
		[
		    'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'sendToQueue' => true,
		],
	];
}

##Advanced usage queue you can override job in your config

    'modules' => [
        ...
        'webhook' => [
            'class' => 'mamadali\webhook\Module',
            'url' => 'https://example.com/webhook',
            'jobNamespace' => 'console\job',
        ],
        ...
    ];

and create WebhookJob in your namespace, like this:

<?php

namespace console\job;

use mamadali\webhook\Module;
use Yii;
use yii\base\BaseObject;
use yii\queue\RetryableJobInterface;

class WebhookJob extends BaseObject implements RetryableJobInterface
{
	/**
	 * @var integer the webhook id
	 */
	public $webhook_id;

	public function execute($queue)
	{
		/**
		 * @var Module
		 */
		$module = Yii::$app->getModule('webhook');
		$module->send($this->webhook_id);
	}


	public function getTtr()
	{
		return 60;
	}

	public function canRetry($attempt, $error)
	{
		return $attempt < 5;
	}
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2022-02-22