studio308/moesif-laravel

Moesif SDK for Laravel 8.*

0.2.2 2023-02-03 21:52 UTC

This package is auto-updated.

Last update: 2024-09-30 01:52:17 UTC


README

Built For Laravel Latest Version on Packagist Total Downloads Software License

Moesif SDK for Laravel 8.*

兼容性

目前仅与Laravel 8.*兼容

安装

通过Composer

$ composer require studio308/moesif-laravel

Laravel使用包自动发现,因此不需要您手动添加ServiceProvider。

配置

您需要提供您的Moesif应用程序ID才能使它正常工作。

您可以从Moesif仪表板 -> 右上角菜单 -> 安装找到您的应用程序ID

如果您不需要更改任何其他配置值,您只需设置适当的env变量即可。应用程序ID所需的必要env变量是

MOESIF_APPLICATION_ID

您可以设置的另一个env变量是

MOESIF_DEBUG

配置文件中为您设置了合理的默认值,但如果您需要修改它,您可以在使用以下命令发布后进行修改以适应您的需求。

php artisan vendor:publish

(请勿将应用程序ID放入配置文件中。请使用env变量。)

用法

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) => 关联数组 可选的,一个函数,它接受一个请求和响应,并返回$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 可选的,一个函数,它接受一个请求和响应,如果此API调用不应发送到Moesif,则返回true。

debug

类型:Boolean 可选的,如果为true,则将使用Illuminate\Support\Facades\Log打印调试消息。

Moesif Laravel SDK的致谢

其他技巧

  • 发送数据的方法是使用带有cURL命令的exec()(即非阻塞方式)。Php exec()命令可能成功,但cURL本身可能有401错误。因此,在集成之后,如果您没有在Moesif仪表板中看到事件和数据,请开启调试选项,那么cURL命令本身将会被记录。您可以执行那个cURL命令并查看问题所在。最常见的是检查应用程序ID是否设置正确。

其他集成

要查看更多关于集成选项的文档,请访问 集成选项文档