1. Go to this page and download the library: Download supashiphq/php-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
return [
'sdk_key' => env('SUPASHIP_SDK_KEY'),
'environment' => env('SUPASHIP_ENVIRONMENT', 'production'),
/**
* Central list of flags and fallbacks — keep in sync with what you use in Supaship.
*/
'features' => [
'new-ui' => false,
'theme-config' => [
'primaryColor' => '#007bff',
'darkMode' => false,
],
],
];
use Illuminate\Support\ServiceProvider;
use Supaship\SupashipClient;
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(SupashipClient::class, function ($app) {
$config = $app['config']->get('supaship');
return new SupashipClient([
'sdkKey' => $config['sdk_key'],
'environment' => $config['environment'],
'features' => $config['features'],
'context' => [
// Filled at boot or request time — see below
'appEnv' => config('app.env'),
],
]);
});
}
}
use Illuminate\Support\Facades\Auth;
use Supaship\SupashipClient;
public function boot(): void
{
$this->app->afterResolving(SupashipClient::class, function (SupashipClient $client) {
$user = Auth::user();
if ($user) {
$client->updateContext([
'userId' => (string) $user->id,
'email' => $user->email ?? '',
]);
}
});
}
use Supaship\SupashipClient;
class DashboardController extends Controller
{
public function __construct(private readonly SupashipClient $features) {}
public function index()
{
$showNewUi = $this->features->getFeature('new-ui');
$theme = $this->features->getFeature('theme-config');
return view('dashboard', compact('showNewUi', 'theme'));
}
}
namespace App\EventSubscriber;
use Supaship\SupashipClient;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
final class SupashipContextSubscriber implements EventSubscriberInterface
{
/** @var SupashipClient */
private $client;
/** @var Security */
private $security;
public function __construct(SupashipClient $client, Security $security)
{
$this->client = $client;
$this->security = $security;
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$user = $this->security->getUser();
if ($user === null) {
return;
}
$this->client->updateContext([
// Adjust to your User class / identifier field
'userId' => method_exists($user, 'getUserIdentifier')
? $user->getUserIdentifier()
: (string) spl_object_id($user),
]);
}
public static function getSubscribedEvents(): array
{
return [KernelEvents::REQUEST => ['onKernelRequest', 8]];
}
}
use Supaship\SupashipClient;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
final class HomeController extends AbstractController
{
#[Route('/', name: 'home')]
public function index(SupashipClient $features): Response
{
$newUi = $features->getFeature('new-ui');
return $this->render('home.html.twig', ['new_ui' => $newUi]);
}
}