Unlock the Power of Dynamic Features in Symfony: The Ultimate Guide to implement Feature Flags

Jakub Skowron (skowron.dev)
4 min readAug 21, 2023

--

Photo by AltumCode on Unsplash

Feature flags, also known as feature toggles, are a programming technique that allows for the dynamic modification of an application’s behavior without the need to change and redeploy its code. Essentially, they are variables that determine whether a specific feature is available or not. Here are some key points about feature flags:

  • Dynamic Control: With feature flags, you can turn features on or off in real-time, without the need for new deployments.
  • Testing in Production: Feature flags allow for the testing of new features directly in the production environment, but only for selected users.
  • Risk Reduction: If a new feature causes issues, a feature flag allows for its immediate deactivation.
  • Personalization: Feature flags can be used to deliver personalized features to specific user groups.
  • Progressive Deployment: Instead of rolling out a new feature to all users at once, it can be gradually introduced.
  • Experiments and A/B Testing: Feature flags enable A/B testing.

Practical Use in Symfony

  • Flagception Bundle

Flagception is an innovative bundle for Symfony that facilitates easy management of feature flags in your project. Here’s what you need to know:

  1. Installation: To install the Flagception Bundle, use the following command:
composer require bestit/flagception-bundle

2. Configuration: After installation, you need to configure the bundle. Here’s a configuration example:

flagception:
features:
my_feature:
default: false
constraints: 'user_id == 12 or (date("H") > 8 and date("H") < 18)'

3. Usage in Controllers: You can now utilize the feature_manager service in your controller:

public function myAction(FeatureManagerInterface $featureManager) {
if ($featureManager->isActive('my_feature')) {
// ...
}
}

4. Usage in Twig: You can also use the feature function in your Twig templates:

{% if feature('my_feature') %}
{# ... #}
{% endif %}

Flagception also offers support for various data sources (e.g., databases, Redis), cookies, and the built-in Symfony profiler.

The profiler allows for real-time monitoring and analysis of flag usage, which is invaluable during testing and debugging. Flagception can be seamlessly integrated with other tools like Unleash, allowing for even more advanced flag management.

  • Unleash Bundle for Symfony

Unleash.io is a powerful tool for managing feature flags that can be easily integrated with various platforms, including Symfony. Importantly, Unleash.io can be self-hosted on your own machine, giving you full control over data and configuration. With this tool, non-developers, such as testers or product owners, can easily manage flags, control their availability for different user groups, and monitor their usage.

Unleash Bundle is a package for Symfony 4.4, 5.4+, and 6+ applications, providing an easy way to implement feature flags using Gitlab Feature Flags. Installation is a piece of cake, just two simple steps:

  1. Download composer package:
composer require stogon/unleash-bundle

2. Add some configuration: Full configuration example:

# config/packages/unleash.yaml
unleash:
api_url: 'https://gitlab.com/api/v4/feature_flags/unleash/<project_id>/'
auth_token: '<auth>'
instance_id: '<some ID>'
environment: '%kernel.environment%'
cache:
enabled: true
service: '@cache.app'
ttl: 15

Usage, just the essentials:

— In Controllers: To use the client, simply inject Stogon\UnleashBundle\UnleashInterface into your service:

namespace App\Controller;
use UnleashBundle\UnleashInterface;

class HomeController {
public function index(UnleashInterface $unleash) {
if ($unleash->isFeatureEnabled('my_awesome_feature')) {
// do something amazing!
}
if ($unleash->isFeatureDisabled('my_other_feature')) {
// do something else
}
return $this->render('home/index.html.twig');
}
}

— In Twig: The package provides Twig functions that allow you to check if a specific feature is enabled/disabled for the current user:

{% if is_feature_enabled('my_awesome_feature') %}
<div class="alert alert-success">The "my_awesome_feature" feature is enabled for the current user!</div>
{% else %}
<div class="alert alert-warning">The "my_awesome_feature" feature is disabled for the current user!</div>
{% endif %}

— Console Commands: The package also provides console commands:

  • unleash:features:fetch - Fetches Unleash features from the server and stores them in cache for later use.
  • unleash:features:list - Displays available Unleash features.

— Strategies: The Unleash Bundle supports various strategies such as default, userWithId, flexibleRollout, gradualRolloutUserId, gradualRolloutSessionId, and gradualRolloutRandom. You can also add your own strategies or override existing ones.

GitLab and Unleash

GitLab utilizes Unleash, a feature toggle service. By enabling or disabling a flag in GitLab, your application can determine which features should be turned on or off. You can create feature flags in GitLab and use the API from your application to retrieve a list of feature flags and their statuses. The application needs to be configured to communicate with GitLab, so it’s up to the developers whether they use a compatible client library and integrate feature flags with the application.

Conclusion

Feature flags are a powerful tool that can significantly accelerate the development process and allow for safe testing of new features in a production environment. With tools like Unleash, Flagception, and GitLab support, managing flags becomes simple and intuitive, even for non-developers.

--

--

Jakub Skowron (skowron.dev)

Poland based PHP/Python Web Backend dev. Love to work with Symfony and FastAPI frameworks. In spare time totally gearhead.