Package Data | |
---|---|
Maintainer Username: | xingwang |
Maintainer Contact: | derric@moesif.com (derric@moesif.com) |
Package Create Date: | 2016-10-24 |
Package Last Update: | 2024-10-30 |
Home Page: | https://www.moesif.com |
Language: | PHP |
License: | Apache-2.0 |
Last Refreshed: | 2024-12-11 15:15:47 |
Package Statistics | |
---|---|
Total Downloads: | 53,102 |
Monthly Downloads: | 1,305 |
Daily Downloads: | 28 |
Total Stars: | 11 |
Total Watchers: | 9 |
Total Forks: | 7 |
Total Open Issues: | 0 |
Official middleware for PHP Laravel (> 5.1) to automatically capture incoming HTTP traffic.
A Moesif SDK is available for Laravel 4.2. Credit for creating this goes to jonnypickett.
Via Composer
$ composer require moesif/moesif-laravel
or add 'moesif/moesif-laravel' to your composer.json file accordingly.
// In config/app.php
'providers' => [
/*
* Application Service Providers...
*/
Moesif\Middleware\MoesifLaravelServiceProvider::class,
];
If website root is your API, add to the root level:
// In App/Http/Kernel.php
protected $middleware = [
/*
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*/
\Moesif\Middleware\MoesifLaravel::class,
];
If API under specific route group, add to your route group:
// In App/Http/Kernel.php
protected $middlewareGroups = [
/**
* The application's API route middleware group.
*/
'api' => [
//
\Moesif\Middleware\MoesifLaravel::class,
],
];
To track only certain routes, use route specific middleware setup.
$ php artisan vendor:publish --provider="Moesif\Middleware\MoesifLaravelServiceProvider"
Edit config/moesif.php
file.
// In config/moesif.php
return [
//
'applicationId' => 'YOUR APPLICATION ID',
];
You can find your Application Id from Moesif Dashboard -> Top Right Menu -> App Setup
For other configuration options, see below.
You can define Moesif configuration options in the config/moesif.php
file. Some of these fields are functions.
Type: String
Required, a string that identifies your application.
Type: ($request, $response) => String
Optional, a function that takes a $request and $response and return a string for userId. Moesif automatically obtains end userId via $request->user()['id'], In case you use a non standard way of injecting user into $request or want to override userId, you can do so with identifyUserId.
// In config/moesif.php
$identifyUserId = function($request, $response) {
// $user = $request->user();
// return $user['id'];
return 'end_user_id';
};
return [
//
'identifyUserId' => $identifyUserId
];
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.
// In config/moesif.php
$getMetadata = function($request, $response) {
return array("foo"=>"laravel example", "boo"=>"custom data");
};
return [
//
'getMetadata' => $getMetadata
];
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.
// In config/moesif.php
$maskRequestHeaders = function($headers) {
$headers['password'] = '****';
return $headers;
};
return [
//
'maskRequestHeaders' => $maskRequestHeaders
];
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.
// In config/moesif.php
$maskRequestBody = function($body) {
// remove any sensitive information.
return $body;
};
return [
//
'maskRequestBody' => $maskRequestBody
];
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
If you are updating the user profile to get visibility. You can use the updateUser
method. This method is attached to the moesif middleware object to update the users profile or metadata.
use Moesif\Middleware\MoesifLaravel;
$user = array(
"user_id" => "phpapiuser",
"metadata" => array(
"email" => "johndoe@acmeinc.com",
"string_field" => "value_1",
"number_field" => 0,
"object_field" => array(
"field_a" => "value_a",
"field_b" => "value_b"
)
),
);
$middleware = new MoesifLaravel();
$middleware->updateUser($user);
// the user_id field is required.
The metadata
field can be any custom data you want to set on the user. The user_id
field is required.
If you are updating the user profile to get visibility. You can use the updateUsersBatch
method. This method is attached to the moesif middleware object to update the users profile or metadata in batch.
use Moesif\Middleware\MoesifLaravel;
$metadata = array(
"email" => "johndoe@acmeinc.com",
"string_field" => "value_1",
"number_field" => 0,
"object_field" => array(
"field_a" => "value_a",
"field_b" => "value_b"
)
);
$userA = array(
"user_id" => "phpapiuser",
"metadata" => $metadata,
);
$userB = array(
"user_id" => "phpapiuser1",
"metadata" => $metadata,
);
$users = array();
$users[] = $userA;
$users[] = $userB;
$middleware = new MoesifLaravel();
$middleware->updateUsersBatch($users);
// the user_id field is required.
The metadata
field can be any custom data you want to set on the user. The user_id
field is required.
To view more more documentation on integration options, please visit the Integration Options Documentation.