Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Simple and powerful Webhooks for Laravel

License

NotificationsYou must be signed in to change notification settings

viezel/webhooks

Repository files navigation

Latest Version on PackagistGitHub 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 theShouldDeliverWebhooks interface.

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

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

useApp\Models\Post;useViezel\Webhooks\Contracts\ShouldDeliverWebhooks;class PostUpdatedEventimplements ShouldDeliverWebhooks{publicfunction__construct(Post$post)    {$this->post =$post;    }publicfunctiongetWebhookName():string    {return'post:updated';    }publicfunctiongetWebhookPayload():array    {return ['post' =>$this->post->toArray(),'extra' => ['foo' =>'bar'            ]               ];    }}

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

publicfunctionboot(){    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

GEThttps://myapp.test/api/hooks/events

List registered webhooks

GEThttps://myapp.test/api/hooks

Register a webhook

POSThttps://myapp.test/api/hooks

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

Delete a webhook

DELETEhttps://myapp.test/api/hooks/{id}

Testing

composertest

Credits

License

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

About

Simple and powerful Webhooks for Laravel

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp