mpociot / captainhook
Add webhooks to your Laravel app.
Installs: 8 900
Dependents: 1
Suggesters: 0
Security: 0
Stars: 335
Watchers: 11
Forks: 27
Open Issues: 7
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~6.0
- illuminate/support: ~5.0
Requires (Dev)
- illuminate/database: ~5.0
- illuminate/events: ~5.0
- mockery/mockery: ~0.9
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~4.7 || ~5.0
This package is auto-updated.
Last update: 2023-11-24 09:21:29 UTC
README
# Captain Hook ## Add Webhooks to your Laravel app, arrrImplement multiple webhooks into your Laravel app using the Laravel Event system.
A webhook is a method of altering the behavior of a web application, with custom callbacks.
These callbacks may be managed by third-party users and developers who may not necessarily
be affiliated with the originating application.
Examples
php artisan hook:add http://www.myapp.com/hooks/ '\App\Events\PodcastWasPurchased' php artisan hook:add http://www.myapp.com/hooks/ 'eloquent.saved: \App\User'
Webhook::create([ "url" => Input::get("url"), "event" => "\\App\\Events\\MyEvent", "tenant_id" => Auth::id() ]);
Contents
## InstallationIn order to add CaptainHook to your project, just add
"mpociot/captainhook": "~2.0"
to your composer.json
's require
block. Then run composer install
or composer update
.
Or run composer require mpociot/captainhook
if you prefer that.
Then in your config/app.php
add
Mpociot\CaptainHook\CaptainHookServiceProvider::class
Publish and run the migration to create the "webhooks" table that will hold all installed webhooks.
php artisan vendor:publish --provider="Mpociot\CaptainHook\CaptainHookServiceProvider" php artisan migrate## Usage
The CaptainHook service provider listens for any eloquent.*
events.
Let's say you want to have a webhook that gets called every time your User model gets updated.
The event that gets called from Laravel will be:
So this will be the event you want to listen for.
### Add new webhooksThis command takes two arguments:
The webhook URL that will receive the POST requests The event name. This could either be one of theeloquent.*
events, or one of your custom events.
php artisan hook:add http://www.myapp.com/hook/ 'eloquent.saved: \App\User'### Delete existing webhooks
php artisan hook:delete 2### List all active webhooks
To list all existing webhooks, use the hook:list
command.
It will output all configured webhooks in a table.
### SparkInstall this package like stated in the Installation section, then follow the Spark installation instructions.
### Custom event listenersAll listeners are defined in the config file located at config/captain_hook.php
.
To receive the event data in your configured webhook, use:
// Retrieve the request's body and parse it as JSON $input = @file_get_contents("php://input"); $event_json = json_decode($input); // Do something with $event_json### Webhook logging
You can configure how many logs will be saved per webhook (Default 50).
This value can be modified in the configuration file config/captain_hook.php
.
To add a webhook that is scoped to the current user, you could do for example:
Webhook::create([ "url" => Input::get("url"), "event" => "\\App\\Events\\MyEvent", "tenant_id" => Auth::id() ]);
Now when you fire this event, you want to call the webhook only for the currently logged in user.
To return only the webhooks for the currently logged in user, it might look like this:
'filter' => function( $webhook ){ return $webhook->tenant_id == Auth::id(); },## License
CaptainHook is free software distributed under the terms of the MIT license.
'Day 02: Table, Lamp & Treasure Map' image licensed under Creative Commons 2.0 - Photo from stevedave