Feature toggles (also called feature switch, feature flag, feature flipper, conditional feature, etc) is a technique used in software development in a continues deployment environment, where features that are not-yet-ready are deployed to production, but are turned off using a config flag. This practice allows developers to deploy unstable code to production, and once the feature is ready or stable, it can just be enabled.
Martin Fowler wrote an article about feature toggles, where he explains how feature toggles work in an agile environment.
But there are a lot of questions on ‘how’ do you implement feature toggles in your project.
I recently wrote a library (called Toggler) that allows you to use feature toggles in almost any PHP project. It also has a Twig extension, to disable certain UI elements, and it has support to integrate into Symfony.
Specifying if a feature should be enabled or disabled, you need to set up the config:
$features = [ 'some-feature' => true, 'another-feature' => false ]; toggleConfig($features);
The config can be used in several ways.
The first way, is to use an ‘if’ statement to check if a feature is enabled or note, and run a piece of code when the feature is enabled
if (toggle('some-feature')) { // some-feature is enabled, and we can run our code here }
Another way is to execute a callback if a feature is enabled
toggle('some-feature', function () { // Code here to execute when 'some-feature' is enabled });
Toggler also comes with a Twig extension, which you can use to disable UI elements in a page. This is useful in the case where you want to hide a link to a page that is not yet available.
The Twig extension allows you to wrap features in a tag, which will hide when the feature is not enabled
{% toggle 'some-feature' %} This content will be hidden when 'some-feature' is not enabled {% endtoggle %}
You can also use the `toggle()` function in a condition
Visit this link
An important note about feature toggles, is to not wrap every piece of code for a feature in a toggle. If you are creating a new page, then just hide the link to that page with a toggle. Or if you are making changes to any business logic, then just wrap the calling function in a toggle. This makes it easier to remove the toggles when all the code is in production and stable.
Leave a Reply