minkbear/captainhook
Composer 安装命令:
composer require minkbear/captainhook
包简介
Add webhooks to your Laravel app.
README 文档
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
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 webhooksIf 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 webhooksTo 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.
### 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
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.
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
minkbear/captainhook 适用场景与选型建议
minkbear/captainhook 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「events」 「HOOK」 「laravel」 「webhook」 「webhooks」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 minkbear/captainhook 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 minkbear/captainhook 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 minkbear/captainhook 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easily manage git hooks in your composer config
Библиотека для реализаций паттерна Pub/Sub
Wireless Cross-Component Communication
A powerful event calendar Tool for Laravel's Nova 5.
A system for organizing WordPress hooks
Command bus implementation: Commands and domain events
统计信息
- 总下载量: 16
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-04