Frontegg is a web platform where SaaS companies can set up their fully managed, scalable and brand aware - SaaS features and integrate them into their SaaS portals in up to 5 lines of code.
Use the package manager Composer to install Frontegg SDK for PHP.
composer require frontegg/php-sdk
Frontegg offers multiple components for integration with the Frontegg's scalable back-end and front-end libraries.
Minimal configuration:
<?php
require_once './vendor/autoload.php';
require_once './src/Frontegg/autoload.php';
use Frontegg\Frontegg;
use Psr\Http\Message\RequestInterface;
$config = [
'clientId' => 'YOUR_CLIENT_ID',
'clientSecret' => 'YOUR_SECRET_API_KEY',
'contextResolver' => function(RequestInterface $request) {
return [
'tenantId' => 'THE-TENANT-ID',
'userId' => 'test-user-id',
'permissions' => [],
];
},
];
$frontegg = new Frontegg($config);
$frontegg->init();
Advanced configuration:
<?php
require_once './vendor/autoload.php';
require_once './src/Frontegg/autoload.php';
use Frontegg\Frontegg;
use Frontegg\HttpClient\FronteggCurlHttpClient;
use Psr\Http\Message\RequestInterface;
$config = [
'clientId' => 'YOUR_CLIENT_ID',
'clientSecret' => 'YOUR_SECRET_API_KEY',
'apiBaseUrl' => 'https://api.frontegg.com/',
'authenticationBaseUrl' => 'https://api.frontegg.com/',
'apiUrls' => [
'authentication' => '/auth/vendor',
'audits' => '/audits',
],
'contextResolver' => function(RequestInterface $request) {
return [
'tenantId' => 'THE-TENANT-ID',
'userId' => 'test-user-id',
'permissions' => [],
];
},
'disableCors' => false, // You can enable/disable CORS headers for Middleware Proxy.
'httpClientHandler' => new FronteggCurlHttpClient(), // You can provide custom HTTP client.
];
$frontegg = new Frontegg($config);
$frontegg->init();
Option Name | Type | Default Value | Note |
---|---|---|---|
clientId | string | None | Client Id. Required |
clientSecret | string | None | API Key. Required |
contextResolver | callable | None | Callback to provide context info. Required |
apiBaseUrl | string | https://api.frontegg.com | Base API URL |
authenticationBaseUrl | string | https://api.frontegg.com | Base URL used for authentication |
apiUrls | array | [] | List of URLs of the API services |
disableCors | bool | false | Disabling CORS headers for Middleware Proxy |
httpClientHandler | special interface* | Curl client** | Custom HTTP client |
apiVersion | string | 'v1' | Not used yet. API version |
*special interface - Frontegg\HttpClient\FronteggHttpClientInterface
,
**Curl client - Frontegg\HttpClient\FronteggCurlHttpClient
Let your customers record the events, activities and changes made to their tenant.
Frontegg’s Managed Audit Logs feature allows a SaaS company to embed an end-to-end working feature in just several lines of code.
<?php
require_once './vendor/autoload.php';
require_once './src/Frontegg/autoload.php';
use Frontegg\Frontegg;
use Frontegg\HttpClient\FronteggCurlHttpClient;
use Psr\Http\Message\RequestInterface;
$config = [
'clientId' => 'YOUR_CLIENT_ID',
'clientSecret' => 'YOUR_SECRET_API_KEY',
'apiBaseUrl' => 'https://api.frontegg.com/',
'apiUrls' => [
'authentication' => '/auth/vendor',
'audits' => '/audits',
],
'contextResolver' => function(RequestInterface $request) {
return [
'tenantId' => 'THE-TENANT-ID',
'userId' => 'test-user-id',
'permissions' => [],
];
},
'disableCors' => false, // You can enable/disable CORS headers for Middleware Proxy.
'httpClientHandler' => new FronteggCurlHttpClient(), // You can provide custom HTTP client.
'apiVersion' => 'v1', // Not used yet. Coming soon.
];
$frontegg = new Frontegg($config);
$auditLog = $frontegg->sendAudit('THE-TENANT-ID', [
'user' => '[email protected]',
'resource' => 'Portal',
'action' => 'Login',
'severity' => 'Info',
'ip' => '123.1.2.3',
]);
<?php
require_once './vendor/autoload.php';
require_once './src/Frontegg/autoload.php';
use Frontegg\Frontegg;
use Frontegg\HttpClient\FronteggCurlHttpClient;
use Psr\Http\Message\RequestInterface;
$config = [
'clientId' => 'YOUR_CLIENT_ID',
'clientSecret' => 'YOUR_SECRET_API_KEY',
'contextResolver' => function(RequestInterface $request) {
return [
'tenantId' => 'THE-TENANT-ID',
'userId' => 'test-user-id',
'permissions' => [],
];
},
'apiBaseUrl' => 'https://api.frontegg.com/',
'apiUrls' => [
'authentication' => '/auth/vendor',
'audits' => '/audits',
],
'httpClientHandler' => new FronteggCurlHttpClient(), // You can provide custom HTTP client.
];
$frontegg = new Frontegg($config);
$auditsLog = $frontegg->getAudits(
'THE-TENANT-ID',
'Text to filter',
0, // Offset
10, // Count
'action', // Field to sort by
'ASC' // Sort direction ('ASC' or 'DESC')
// ... Additional filters
);
Events triggering is easy and maximum configurable for different notification channels.
require_once './vendor/autoload.php';
require_once './src/Frontegg/autoload.php';
use Frontegg\Events\Config\ChannelsConfig;
use Frontegg\Events\Config\DefaultProperties;
use Frontegg\Events\Config\TriggerOptions;
use Frontegg\Events\Channel\WebHookBody;
use Frontegg\Frontegg;
use Psr\Http\Message\RequestInterface;
$clientId = 'YOUR_CLIENT_ID';
$apikey = 'YOUR_API_KEY';
$config = [
'clientId' => $clientId,
'clientSecret' => $apikey,
'contextResolver' => function(RequestInterface $request) {
return [
'tenantId' => 'THE-TENANT-ID',
'userId' => 'test-user-id',
'permissions' => [],
];
},
];
$frontegg = new Frontegg($config);
$triggerOptions = new TriggerOptions(
'eventKeyForTest',
new DefaultProperties(
'Default title',
'Default description',
[
'name' => 'Policy 4',
'id' => '11223456783245234',
]
),
new ChannelsConfig(
new WebHookBody([
'title' => 'Test title!',
])
),
'YOUR_TENANT_ID'
);
$response = $frontegg->triggerEvent($triggerOptions);
The Frontegg Proxy forwards requests to the Frontegg API and pass back responses.
There is no Middleware (filters mechanism for HTTP request) in raw PHP, but in some frameworks it is. For example, in Laravel.
Here you can see example for raw PHP. You can easily adapt it for your framework with Middleware (for Laravel see https://github.com/frontegg/samples/tree/master/frontegg-laravel-starter).
<?php
require_once './vendor/autoload.php';
require_once './src/Frontegg/autoload.php';
use Frontegg\Frontegg;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
/**
* Setup routing rule for "/frontegg" URIs.
* Can be a part of middleware f.e. in Laravel.
*/
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/frontegg') === 0) {
$request = new Request('POST', $_SERVER['REQUEST_URI']);
$response = handleFronteggUri($request);
}
// ...
function handleFronteggUri(RequestInterface $request)
{
$clientId = 'YOUR_CLIENT_ID';
$apikey = 'YOUR_API_KEY';
$config = [
'clientId' => $clientId,
'clientSecret' => $apikey,
'apiBaseUrl' => 'https://dev-api.frontegg.com/',
'contextResolver' => function(RequestInterface $request) {
return [
'tenantId' => 'THE-TENANT-ID',
'userId' => 'test-user-id',
'permissions' => [],
];
},
'disableCors' => false,
];
$frontegg = new Frontegg($config);
$response = $frontegg->forward($request);
return $response->getBody();
}
Another example of raw PHP and with adapting request parameters to Psr-7 Request object:
<?php
use Frontegg\Frontegg;
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\Psr7\Request;
$requestMethod = $_SERVER['REQUEST_METHOD'];
$requestUri = str_replace('/api/frontegg', '', $_SERVER['REQUEST_URI']);
$httpHeaders = getallheaders();
$body = http_build_query($requestMethod === 'GET' ? $_GET : $_POST);
// Here you can see how to setup Psr-7 Request object.
$adapterRequest = new Request($requestMethod, $requestUri, $httpHeaders, $body);
$clientId = 'CLIENT_ID';
$apikey = 'API_KEY';
$tenantId = 'THE_TENANT_ID';
$config = [
'clientId' => $clientId,
'clientSecret' => $apikey,
'apiBaseUrl' => 'https://api.test.com/',
'contextResolver' => function(RequestInterface $request) use ($tenantId) {
return [
'tenantId' => $tenantId,
'userId' => 'test-user-id',
'permissions' => [],
];
},
'disableCors' => false,
];
$frontegg = new Frontegg($config);
$response = $frontegg->forward($adapterRequest);
print $response->getBody();