syzygypl/kunstmaan-feature-switches-bundle

1.0.2 2018-08-07 11:25 UTC

This package is not auto-updated.

Last update: 2024-04-22 05:17:27 UTC


README

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require syzygypl/kunstmaan-feature-switches-bundle

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new  SZG\KunstmaanFeatureSwitchesBundle\KunstmaanFeatureSwitchesBundle(),
        );

        // ...
    }

    // ...
}

Step 3: Generate database schema

bin/console doctrine:migration:diff bin/console doctrine:migration:migrate

Step 4: Import routes

// app/config/routing.yml
KunstmaanFeatureSwitchesBundle:
    resource: "@KunstmaanFeatureSwitchesBundle/Resources/config/routing.yml"
    prefix:   /{_locale}/admin/
    requirements:
        _locale: "%requiredlocales%"

Usage

CREATE A NEW SWITCH

Just enter the /{locale}/admin/settings and select "Feature switches" from the sidebar menu. Then use a big blue button in the right top cornenr. Name and code have to be unique.

TWIG

{% if is_granted('my_special_feature', 'features') %} ENABLED {% endif %}

PHP

In a controller:

/// ...
$this->denyAccessUnlessGranted('my_special_feature', 'features');
// ...

Best practices

Create a constants collection for all switches

Create a new file: app/Features.php

// app/Features.php

<?php

class Features
{
    const MY_SPECIAL = 'my_special_feature';
}

Require just created file in autoloader:

// app/autoload.php
// ...
require __DIR__.'/Features.php';
// ...

Now you can use constants in twig:

{% if is_granted(constant(Features::MY_SPECIAL), 'features') %} ENABLED {% endif %}