PHPackages                             mamadali/yii2-webhook - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mamadali/yii2-webhook

ActiveYii2-extension

mamadali/yii2-webhook
=====================

Yii2 send create and changes to webhook

v1.51(3y ago)1289↓100%Apache-2.0PHP

Since Feb 22Pushed 3y ago1 watchersCompare

[ Source](https://github.com/MamadAliGit/yii2-webhook)[ Packagist](https://packagist.org/packages/mamadali/yii2-webhook)[ RSS](/packages/mamadali-yii2-webhook/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (3)Versions (10)Used By (0)

Yii2 Webhook Behavior
=====================

[](#yii2-webhook-behavior)

Helps you to send models changes to a webhook url

[![Latest Stable Version](https://camo.githubusercontent.com/3e3604b93c8f8fa3b75369a3d4b2b3f0fa60daa2cdf7b916a1311372658ed752/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d616d6164616c692f796969322d776562686f6f6b2e737667)](https://packagist.org/packages/mamadali/yii2-webhook)[![Total Downloads](https://camo.githubusercontent.com/27cd32477bd4746245944b90b35b730c689289ecc3ebd019acfc4c9abb71c49f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d616d6164616c692f796969322d776562686f6f6b2e737667)](https://packagist.org/packages/mamadali/yii2-webhook)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

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:

[](#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](https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/README.md)

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:

```
