• Stars
    star
    335
  • Rank 125,904 (Top 3 %)
  • Language
    PHP
  • Created about 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Add Webhooks to your Laravel app, arrr

Captain Hook

# Captain Hook ## Add Webhooks to your Laravel app, arrr

image image codecov.io Scrutinizer Code Quality Build Status StyleCI

Implement 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

## Installation

In 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

to the providers array.

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.

If the package finds a configured webhook for an event, it will make a POST request to the specified URL.

Webhook data is sent as JSON in the POST request body. The full event object is included and can be used directly, after parsing the JSON body.

Example

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:

eloquent.updated: \App\User

So this will be the event you want to listen for.

### Add new webhooks

If you know which event you want to listen to, you can add a new webhook by using the hook:add artisan command.

This command takes two arguments:

  • The webhook URL that will receive the POST requests
  • The event name. This could either be one of the eloquent.* events, or one of your custom events.
php artisan hook:add http://www.myapp.com/hook/ 'eloquent.saved: \App\User'

You can also add multiple webhooks for the same event, as all configured webhooks will get called asynchronously.

### Delete existing webhooks

To remove an existing webhook from the system, use the hook:delete command. This command takes the webhook ID as an argument.

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.

### Spark

Install this package like stated in the Installation section, then follow the Spark installation instructions.

### Custom event listeners

All listeners are defined in the config file located at config/captain_hook.php.

### Receiving a webhook notification

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

Starting with version 2.0, this package allows you to log the payload and response of the triggered webhooks.

NOTE: A non-blocking queue driver (not sync) is highly recommended. Otherwise your application will need to wait for the webhook execution.

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.

### Using webhooks with multi tenancy

Sometimes you don't want to use system wide webhooks, but rather want them scoped to a specific "tenant". This could be bound to a user or a team.

The webhook table has a field tenant_id for this purpose. So if you want your users to be able to add their own webhooks, you won't use the artisan commands to add webhooks to the database, but add them on your own.

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.

In order to filter the webhooks, modify the filter configuration value in the config/captain_hook.php file. This filter is a Laravel collection filter.

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

More Repositories

1

chatgpt-vscode

A VSCode extension that allows you to use ChatGPT
TypeScript
3,779
star
2

laravel-apidoc-generator

Laravel API Documentation Generator
PHP
3,386
star
3

whiteboard

Simply write beautiful API documentation.
JavaScript
1,224
star
4

teamwork

User to Team associations with invitation system for the Laravel 5 Framework
PHP
1,036
star
5

laravel-test-factory-helper

Generate Laravel test factories from your existing models
PHP
930
star
6

versionable

Laravel Model versioning made easy
PHP
687
star
7

laravel-testtools

Chrome extension to generate Laravel integration tests while using your app.
JavaScript
473
star
8

documentarian

Create beautiful API documentation
JavaScript
377
star
9

reauthenticate

Reauthenticate users by letting them re-enter their passwords for specific parts of your app.
PHP
296
star
10

laravel-firebase-sync

Synchronize your Eloquent models with a Firebase Realtime Database.
PHP
264
star
11

laravel-face-auth

Laravel Face authentication
PHP
157
star
12

TiSideMenu

iOS 7 / 8 style side menu with parallax effect. (Wrapper module for RESideMenu)
Objective-C
147
star
13

pipeline

Simple PHP pipelines
PHP
131
star
14

reanimate

Easily add an "undo" option to your Laravel application
PHP
121
star
15

titanium-facebook-slide-menu

Example of how to implement a facebook like sliding menu in appcelerator's titanium mobile
JavaScript
117
star
16

blacksmith

The unofficial Laravel Forge PHP API
PHP
103
star
17

human-regex

Regular expressions for human beings, not machines
PHP
102
star
18

reflection-docblock

Fork of phpdocumentor/reflection-docblock to work with the api-documentation-generator
PHP
85
star
19

TiMotionEffects

UIMotionEffects for Appcelerator Titanium
Python
59
star
20

TiMWKProgressIndicator

A minimal progress indicator for iOS with status update support. Displays above UINavigationBar and below the Status Bar.
Objective-C
49
star
21

TiCircularSlider

Titanium circular slider module with many customizations.
Objective-C
47
star
22

TiCircularProgress

Circular progress indicator for Appcelerator Titanium
Objective-C
44
star
23

cockpit

Custom widgets for your Laravel app
PHP
38
star
24

titanium-social-modul

iOS6 module for facebook, twitter, sina weibo to use with appcelerator titanium
Python
38
star
25

laravel-slack-inviter

A little Laravel application to invite a user into your Slack team.
PHP
36
star
26

TiImageFromGIF

Create native imageviews from GIFs
Objective-C
35
star
27

TiCustomTab

Create Tabs with custom selected / unselected images.
Objective-C
34
star
28

laravel-composite-key

PHP trait to use composite keys in your Laravel Eloquent models.
PHP
34
star
29

codeception-testtools

Chrome extension to generate Codeception acceptance tests while using your app.
JavaScript
34
star
30

TiAndroidAutofocus

Prevents TextFields in Android to autofocus
Java
31
star
31

titanium-mobile-twitter-module

Basic twitter iOS 5 integration as a titanium module
Objective-C
30
star
32

TiAppirater

Titanium mobile iOS rating reminder
Objective-C
27
star
33

TiFaceRecognizer

CoreImage face detection module to use with the Appcelerator Titanium SDK
Python
27
star
34

titanium-mobile-alasset-module

Titanium module to get access to the iOS ALAsset framework
Objective-C
26
star
35

TiiMMAppSwitcher

iOS Titanium Mobile module - Replace iOS7 app switcher view with your own customized card view.
Objective-C
26
star
36

titanium-mobile-zip-module

zip module for appcelerators titanium mobile
C
25
star
37

chuck-norris-jokes

Create random Chuck Norris jokes.
PHP
25
star
38

TiScreenshotDetection

Detect when someone takes a screenshot in your Appcelerator Titanium iOS app.
Objective-C
20
star
39

TokenCompleteTextView

Java
20
star
40

lastfm-slack

Set your Last.fm tracks as your Slack status
PHP
19
star
41

TiAdvancedSplitWindow

Appcelerator titanium module to make use of the MGSplitView functions, not used by the Titanium SDK
Python
19
star
42

socialite-slack

Slack OAuth2 Provider for Laravel Socialite
PHP
18
star
43

TiTutorialView

Path like tutorial view as a commonJS module for Appcelerator Titanium
JavaScript
18
star
44

TiWebviewFragment

Simple helper class that is a workaround for Webviews Crashing / rendering blank on Android 4.4 in Titanium
Java
17
star
45

laravel-firebase-sync-example

Example project demonstrating the usage of the laravel-firebase-sync package
PHP
17
star
46

TiSplashScreen

Path like splash screen animation
Python
15
star
47

TiSDSegmentedControl

This is a titanium module for Olivier Poitrey's SDSegmentedControl library.
Objective-C
14
star
48

TiDocumentInteraction

Share/Print/Open files with the native activity view controller in your Titanium app
Python
12
star
49

mpociot

10
star
50

TiSMPageControl

Titanium mobile iOS module for some funky ScrollableView customization
Objective-C
7
star
51

llamero

A GUI application to easily try out Facebook's LLaMA models.
JavaScript
6
star
52

botman-spark-demo

BotMan + Cisco Spark demo project
PHP
5
star
53

psd2tss

JavaScript
5
star
54

feature-switch

A feature switcher for PHP
PHP
5
star
55

MagicPie

A Titanium implementation of https://github.com/Sk0rpion/MagicPie
Python
4
star
56

ti-crittercism-hook

A CLI hook for Appcelerator Titanium to automatically upload dSYM files to crittercism.
JavaScript
4
star
57

skeleton-php

PHP
3
star
58

TiIOS8WebviewFix

Prevent iOS8 iPad apps with WebViews from crashing
Python
3
star
59

laracon-eu-2017

My Laracon EU 2017 talk
PHP
3
star
60

php-uk-conference-2018

My PHP UK Conference 2018 Talk
PHP
3
star
61

titanium-twitter-oauth

A Twitter OAuth Module for Titanium Mobile
JavaScript
3
star
62

php-serbia-2018

The talk I gave at PHP Serbia 2018
PHP
2
star
63

ti-taffy

taffyDB wrapper for titanium
JavaScript
2
star
64

botman-workshop

PHP
2
star
65

TiShowcaseView

Native android module for Appcelerator Titanium that tries to wrap the "ShowcaseView" library.
2
star