Using feature flags in Symfony (supashiphq/php-sdk)
Register Supaship\SupaClient as a Symfony service with %env()%-backed parameters, then attach the current user in a KernelEvents::REQUEST subscriber so feature targeting receives user id and other context. The PHP SDK supports PHP 7.4+; adjust type hints, constructor visibility, and routing attributes to your Symfony and PHP versions.
Prerequisites: PHP SDK overview — Composer package supashiphq/php-sdk, Server SDK key.
1. Environment variables
In .env.local (do not commit secrets):
SUPASHIP_SDK_KEY=your-sdk-key
SUPASHIP_ENVIRONMENT=production2. Symfony parameters (config/services.yaml)
Symfony 6/7-style example:
parameters:
supaship.sdk_key: '%env(SUPASHIP_SDK_KEY)%'
supaship.environment: '%env(SUPASHIP_ENVIRONMENT)%'
supaship.features:
new-ui: false
theme-config:
primaryColor: '#007bff'
darkMode: falseFor large fallback maps, use imports: or load the array from PHP — the structure must match what you pass into SupaClient.
3. Service definition for SupaClient
services:
Supaship\SupaClient:
class: Supaship\SupaClient
arguments:
- {
sdkKey: '%supaship.sdk_key%',
environment: '%supaship.environment%',
features: '%supaship.features%',
context: { appEnv: '%kernel.environment%' },
}
public: true4. Event subscriber (per-request user context)
<?php
namespace App\EventSubscriber;
use Supaship\SupaClient;
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
{
public function __construct(
private SupaClient $client,
private Security $security,
) {
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$user = $this->security->getUser();
if ($user === null) {
return;
}
$this->client->updateContext([
'userId' => method_exists($user, 'getUserIdentifier')
? $user->getUserIdentifier()
: (string) spl_object_id($user),
]);
}
public static function getSubscribedEvents(): array
{
return [KernelEvents::REQUEST => ['onKernelRequest', 8]];
}
}If App\ is autowired, Symfony discovers the subscriber; otherwise add the kernel.event_subscriber service tag.
5. Controller with SupaClient injection
use Supaship\SupaClient;
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(SupaClient $features): Response
{
$newUi = $features->getFeature('new-ui');
return $this->render('home.html.twig', ['new_ui' => $newUi]);
}
}Last updated on