jonnypickett / moesif-laravel
Requires
- php: >=7.3.0
- illuminate/support: 8.*
- nesbot/carbon: ^2.66
Requires (Dev)
- ext-json: *
- orchestra/testbench: 6.*
- phpunit/phpunit: 9.*
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2023-02-03 21:52:41 UTC
README
Moesif SDK for Laravel 8.*
兼容性
目前仅兼容 Laravel 8.*
安装
通过 Composer
$ composer require studio308/moesif-laravel
Laravel 使用包自动发现,因此不需要您手动添加 ServiceProvider。
配置
您需要提供您的 Moesif 应用程序 ID 以使其工作。
您可以在 Moesif 控制台 -> 右上角菜单 -> 安装 中找到您的应用程序 ID。
如果您不需要更改其他配置值,只需设置适当的环境变量即可。应用程序 ID 所需的环境变量是
MOESIF_APPLICATION_ID
您可以设置的另一个环境变量是
MOESIF_DEBUG
配置文件中已为您设置了合理的默认值,但如果您需要修改它,可以在使用以下命令发布后修改以适应您的需求。
php artisan vendor:publish
(请勿将您的应用程序 ID 放入配置文件中。使用环境变量。)
用法
将 moesif
中间件添加到您的 Http/Kernel
中的 api
中间件组
/** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ ... ], 'api' => [ ... 'moesif', ... ], ];
配置文档
以下是示例 config/moesif.php
配置文件。
<?php return array( /* |-------------------------------------------------------------------------- | Moesif Application ID |-------------------------------------------------------------------------- | | This is the Moesif application id. | */ 'applicationId' => env('MOESIF_APPLICATION_ID'), /* |-------------------------------------------------------------------------- | Skip |-------------------------------------------------------------------------- | | Return true if the event is to be skipped. | */ 'skip' => function ($request, $response) { $host = explode('.', $request->server('HTTP_HOST')); return $host[0] != 'api'; }, /* |-------------------------------------------------------------------------- | Mask Request Headers |-------------------------------------------------------------------------- | | Add or remove request headers. | */ 'maskRequestHeaders' => function ($headers) { if (isset($headers['authorization'])) { $headers['authorization'] = str_repeat('*', 18); } return $headers; }, /* |-------------------------------------------------------------------------- | Mask Request Body |-------------------------------------------------------------------------- | | Remove any fields from body that you don't want sent to Moesif. | */ 'maskRequestBody' => function ($body) { if (isset($body['password'])) { $body['password'] = str_repeat('*', 18); } return $body; }, /* |-------------------------------------------------------------------------- | Mask Response Headers |-------------------------------------------------------------------------- | | Add or remove response headers. | */ 'maskResponseHeaders' => function ($headers) { return $headers; }, /* |-------------------------------------------------------------------------- | Mask Response Body |-------------------------------------------------------------------------- | | Remove any fields from body that you don't want sent to Moesif. | */ 'maskResponseBody' => function ($body) { if (isset($body['token'])) { $body['token'] = str_repeat('*', 18); } return $body; }, /* |-------------------------------------------------------------------------- | Identify User ID |-------------------------------------------------------------------------- | | Identify the user. | */ 'identifyUserId' => function ($request, $response) { return null; }, /* |-------------------------------------------------------------------------- | Identify Session ID |-------------------------------------------------------------------------- | | Identify the session. | */ 'identifySessionId' => function ($request, $response) { if ($request->hasSession()) { return $request->session()->getId(); } else { return null; } }, /* |-------------------------------------------------------------------------- | Meta Data |-------------------------------------------------------------------------- | | Add any extra data to be sent to Moesif. | */ 'getMetaData' => function ($request, $response) { return []; }, /* |-------------------------------------------------------------------------- | Tags |-------------------------------------------------------------------------- | | Add any tags to be sent to Moesif. | */ 'addTags' => function ($request, $response) { return ''; }, /* |-------------------------------------------------------------------------- | API Version |-------------------------------------------------------------------------- | | Identify the API Version. | */ 'apiVersion' => function ($request, $response) { return null; }, /* |-------------------------------------------------------------------------- | Debug |-------------------------------------------------------------------------- | | Set to true to see debug information. | */ 'debug' => env('MOESIF_DEBUG', false), );
以下是对各种配置值的说明。
applicationId
类型: String
必需的,一个标识您的应用程序的字符串。
identifyUserId
类型: ($request, $response) => String
可选的,一个函数,它接受一个请求和一个响应并返回一个表示 userId 的字符串。
identifyCompany
类型: ($request, $response) => String
可选的,一个函数,它接受一个请求和一个响应并返回一个表示 companyId 的字符串。
identifySessionId
类型: ($request, $response) => String
可选的,一个函数,它接受一个请求和一个响应并返回一个表示 sessionId 的字符串。Moesif 会自动会话化您的数据,但如果您对结果不满意,可以通过 identifySessionId 来覆盖。
getMetadata
类型: ($request, $response) => Associative Array
可选的,一个函数,它接受一个请求和一个响应并返回 $metdata,它是一个表示 JSON 的关联数组。
apiVersion
类型: String
可选的,一个字符串,用于指定 API 版本,例如 1.0.1,以便更容易进行筛选。
maskRequestHeaders
类型:$headers => $headers
可选,一个函数,它接收一个关联数组 $headers,并返回一个去除/屏蔽敏感头部的关联数组。
maskRequestBody
类型:$body => $body
可选,一个函数,它接收一个 $body,这是一个 JSON 的关联数组表示形式,并返回一个去除任何信息的关联数组。
maskResponseHeaders
类型:$headers => $headers
可选,与上面相同,但用于响应。
maskResponseBody
类型:$body => $body
可选,与上面相同,但用于响应。
skip
类型:($request, $response) => String
可选,一个函数,它接收一个 $request 和 $response,如果此 API 调用不应该发送到 Moesif,则返回 true。
debug
类型:Boolean
可选,如果为 true,则使用 Illuminate\Support\Facades\Log 打印调试信息。
Moesif Laravel SDK 致谢
- 我所做的就是修改了 Moesif 的现有 Laravel 包 以兼容 Laravel 8.*。所有实际重要的贡献都应该归功于 Xing Wang。
其他提示
- 发送数据的分叉(即非阻塞方式)是使用 exec() 和 cURL 命令。Php exec() 命令可能成功,但 cURL 本身可能有 401 错误。因此,集成后,如果您在 Moesif Dash 中看不到事件和数据,请启用调试选项,那么 cURL 命令本身将被记录。您可以执行该 cURL 命令并查看问题所在。最常见的问题是检查应用程序 ID 是否设置正确。
其他集成
要查看更多关于集成选项的文档,请访问 集成选项文档。