laravel-webhooks
Laravel package to handle Webhooks, (*1)
Installation guide
Include the page via Composer:, (*2)
composer require jurihub/laravel-webhooks
Add the Webhooks service provider to your config/app.php
file in the providers
array:, (*3)
Jurihub\LaravelWebhooks\WebhooksServiceProvider::class
, (*4)
To use a Facade instead of injecting the class itself, add this to the aliases
array in the same file:, (*5)
'Webhooks' => Jurihub\LaravelWebhooks\WebhooksFacade::class
, (*6)
Publish the configuration file:, (*7)
php artisan vendor:publish --provider="Jurihub\LaravelWebhooks\WebhooksServiceProvider" --tag="config"
, (*8)
You certainly will want to add some endpoints, list them in the targets
array in config/webhooks.php
, (*9)
Launch the migrations (provided automatically by the ServiceProvider), that will create 2 new tables: webhooks
and webhook_tries
, (*10)
php artisan migrate
, (*11)
The first attempt is sent automatically, but if you want to automatize retries, add the following schedule in your app/Console/Kernel.php
file:, (*12)
$schedule->call(function () {
\Jurihub\LaravelWebhooks\Http\Controllers\Webhooks\SenderController::retry();
})->everyMinute();
To handle incoming webhooks, create a new controller, eg. App\Http\Controllers\Webhooks\ReceiverController.php
, (*13)
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'
}
}
And add the route to your routes/api.php
file to receive the incoming webhooks.
You may want to customize the endpoint, according to the targets
listed in the config/app.php
file., (*14)
Route::post('webhook', 'Webhooks\ReceiverController@handleWebhook');
, (*15)
Activate the webhooks' sending queue:, (*16)
php artisan queue:work database --queue=webhook --tries=3
, (*17)