Feature Toggles in Symfony
When we launched our feature toggle library to the public a few weeks ago, we had multiple people asking for a Symfony Bundle. So guess what? We created one just for you! This bundle doesn't just help you with feature toggling in your controllers, it is shipped with a Twig Extension for your toggling pleasure. Read on to see how to get started!
Installation
Install the bundle with composer and add it to your Kernel.
$ composer require 'qandidate/toggle-bundle' '~0.1'
// AppKernel.php
$bundles = array(
// ..
new Qandidate\Bundle\ToggleBundle\QandidateToggleBundle(),
);
Configuration
The toggle library provides two collections to store your toggles in: the
InMemoryCollection
and the PredisCollection
. The bundle provides a
configuration entry for each of them.
qandidate_toggle:
persistence: redis # or in_memory, which is also the default
If you decided to use Predis you also have to
configure which service to use for the Predis\Client
and which namespace to
use for your Redis keyset:
qandidate_toggle:
persistence: redis
redis_client: redis_client_service_id
redis_namespace: toggle_%kernel.environment% # default
As explained in our earlier post we use a Context
object to test toggles against runtime variables. By injecting a
ContextFactory
you can always create the Context on the moment you need it.
But as each application has different variables to use in the Context
, most
applications will need to create their own factory. The bundle does provides a
default factory, but it only contains the username of the current user.
Register your factory as a service and configure it like this:
qandidate_toggle:
context_factory: acme.your_context_factory.service_id
Usage
The bundle defines multiple services to help you, like a service for the
ToggleManager
and the ContextFactory
.
It also defines a Twig extension, which makes using the bundle in your templates even easier:
{% if is_active('internal_notes') %}
{# Show interal notes only to your employees #}
{% endif %}
With this bundle it is really simple to use feature toggles in your Symfony application, so check it out on github. We would love to hear your feedback in #qandidate on freenode.