studio308 / moesif-laravel
Moesif SDK for Laravel 8.*
Installs: 1 455
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 5
Open Issues: 0
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: 2025-01-30 02:37:08 UTC
README
Moesif SDK for Laravel 8.*
Compatibility
Currently only compatible with Laravel 8.*
Installation
Via Composer
$ composer require studio308/moesif-laravel
Laravel uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.
Configuration
You will need to provide your Moesif Application ID in order to get this working.
You can find your Application ID from the Moesif Dashboard -> Top Right Menu -> Installation
If you don't need to change any other config values, you can get away with just setting the appropriate env variable. The necessary env variable for the Application ID is:
MOESIF_APPLICATION_ID
The other env variable that you can set is:
MOESIF_DEBUG
Some sensible defaults are set for you in the config file, but if you need to modify it, you can modify it to fit your needs after publishing it with the command below.
php artisan vendor:publish
(DO NOT PUT YOUR APPLICATION ID IN THE CONFIG FILE. USE THE ENV VARIABLE.)
Usage
Add the moesif
middleware to the api
middleware group in your Http/Kernel
/** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ ... ], 'api' => [ ... 'moesif', ... ], ];
Configuration Documentation
Below is an example config/moesif.php
configuration file.
<?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), );
Documentation for the various config values is below.
applicationId
Type: String
Required, a string that identifies your application.
identifyUserId
Type: ($request, $response) => String
Optional, a function that takes a $request and $response and return a string for userId.
identifyCompany
Type: ($request, $response) => String
Optional, a function that takes a $request and $response and return a string for companyId.
identifySessionId
Type: ($request, $response) => String
Optional, a function that takes a $request and $response and return a string for sessionId. Moesif automatically sessionizes by processing at your data, but you can override this via identifySessionId if you're not happy with the results.
getMetadata
Type: ($request, $response) => Associative Array
Optional, a function that takes a $request and $response and returns $metdata which is an associative array representation of JSON.
apiVersion
Type: String
Optional, a string to specifiy an API Version such as 1.0.1, allowing easier filters.
maskRequestHeaders
Type: $headers => $headers
Optional, a function that takes a $headers, which is an associative array, and
returns an associative array with your sensitive headers removed/masked.
maskRequestBody
Type: $body => $body
Optional, a function that takes a $body, which is an associative array representation of JSON, and
returns an associative array with any information removed.
maskResponseHeaders
Type: $headers => $headers
Optional, same as above, but for Responses.
maskResponseBody
Type: $body => $body
Optional, same as above, but for Responses.
skip
Type: ($request, $response) => String
Optional, a function that takes a $request and $response and returns true if
this API call should be not be sent to Moesif.
debug
Type: Boolean
Optional, If true, will print debug messages using Illuminate\Support\Facades\Log
Credits for Moesif Laravel SDK
- All I did was modify Moesif's existing Laravel package to be compatible with Laravel 8.*. All credit for what actually matters should be given to Xing Wang.
Additional Tips:
- The forked (i.e. non-blocking way) of sending data is using exec() with a cURL command. The Php exec() command can be successful but the cURL itself may have 401 errors. So after integration, if you don't see events and data show up in your Moesif Dash. Please turn on debug option, then the cURL command itself will be logged. You can execute that cURL command and see what the issues are. The most common thing to check is if the Application ID is set correctly.
Other integrations
To view more more documentation on integration options, please visit the Integration Options Documentation.