obrignoni / webhooks
此Laravel扩展包帮助您组织、安全地映射webhooks到事件。
此包的规范仓库似乎已丢失,因此该包已被冻结。
Requires
- laravel/framework: ^5.3
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ~5.4
This package is not auto-updated.
Last update: 2020-02-27 21:17:11 UTC
README
此Laravel扩展包帮助您组织、安全地映射webhooks到事件。
安装
要开始使用,请通过Composer包管理器安装Laravel Webhooks
composer require obrignoni/webhooks
接下来,在config/app.php配置文件的providers数组中注册Webhooks服务提供器
Obrignoni\Webhooks\WebhooksServiceProvider::class,
为什么要使用webhooks包?
我想...
- 将我的应用程序与多个服务的webhooks集成。
- 保持webhooks的组织和安全。
- 将webhook事件映射到Laravel事件。
用法
以Github Webhooks为例。
Github webhook的有效负载看起来像这样...
POST /payload HTTP/1.1
Host: localhost:4567
X-Github-Delivery: 72d3162e-cc78-11e3-81ab-4c9367dc0958
User-Agent: GitHub-Hookshot/044aadd
Content-Type: application/json
Content-Length: 6615
X-GitHub-Event: issues
{
"action": "opened",
"issue": {
"url": "https://api.github.com/repos/octocat/Hello-World/issues/1347",
"number": 1347,
...
},
"repository" : {
"id": 1296269,
"full_name": "octocat/Hello-World",
"owner": {
"login": "octocat",
"id": 1,
...
},
...
},
"sender": {
"login": "octocat",
"id": 1,
...
}
}
注意,X-GitHub-Event
头部包含issues
,这是Github Events之一。
我们可以使用Artisan命令为github设置webhook。
php artisan make:webhook github
这将创建App\Http\Webhooks\Github
类。
webhook类扩展了WebhookRequest,它也扩展了FormRequest并添加了一些额外的配置选项。
让我们看看生成的类。
<?php namespace App\Http\Webhooks; use Obrignoni\Webhooks\Http\WebhookRequest; class Github extends WebhookRequest { protected $eventField = ''; protected $events = []; protected $authorization = null; public function authorize() { // Authorize the webhook the same way you would with a FormRequest. return false; } public function rules() { return [ ]; } }
我们可以这样设置我们的Github webhook...
<?php namespace App\Http\Webhooks; use Obrignoni\Webhooks\Authorization\GithubAuthorization; use Obrignoni\Webhooks\Http\WebhookRequest; class Github extends WebhookRequest { protected $eventField = 'X-GitHub-Event'; protected $authorization = GithubAuthorization::class; }
事件字段
事件字段是包含事件值的请求参数或头部。
授权处理程序
授权处理程序可以包含授权请求的逻辑。它应返回一个布尔值。
$authorization
被设置为与该包一起包含的GithubAuthorization
处理程序。使用授权处理程序是可选的,并且可以用作authorize
的替代。
映射事件
您使用$events
数组将每个webhook事件映射到事件类。如果为空,事件名称将自动转换为studly cased类。对于Github webhook,pull_request
事件将转换为App\Events\GithubPullRequest
。
以下是如何设置自定义事件的示例。
<?php namespace App\Http\Webhooks; use Obrignoni\Webhooks\Authorization\GithubAuthorization; use Obrignoni\Webhooks\Http\WebhookRequest; class Github extends WebhookRequest { protected $field = 'X-GitHub-Event'; protected $authorization = GithubAuthorization::class; protected $events = [ 'issue_comment' => 'App\Events\SomebodyMadeACommentOnGithub', 'pull_request' => 'App\Events\SomebodySubmittedAPullRequest', ]; }
Webhook请求扩展表单请求
您可以选择像表单请求一样使用authorize
和rules
方法。