Installation:
composer require ecn/featuretoggle-bundle twig/twig # Include Twig if using Twig integration
Add to config/bundles.php:
return [
// ...
Ecn\FeatureToggleBundle\EcnFeatureToggleBundle::class => ['all' => true],
];
Define Features:
Create config/features.yml (add to .gitignore):
ecn_feature_toggle:
features:
new_ui: false
experimental_search: true
dark_mode: false
First Use Case: Check a toggle in a controller:
use Ecn\FeatureToggleBundle\FeatureToggle;
class HomeController extends AbstractController {
public function index(FeatureToggle $toggle) {
if ($toggle->isEnabled('new_ui')) {
return $this->render('home/new_ui.html.twig');
}
return $this->render('home/default.html.twig');
}
}
Runtime Toggle Management:
FeatureToggle service to check toggles dynamically:
$toggle->isEnabled('feature_name'); // Returns bool
$toggle->isDisabled('feature_name'); // Inverse check
{% if 'new_ui' is enabled %}
<div class="ui-feature">...</div>
{% endif %}
Environment-Specific Config:
features.yml per environment (e.g., features.prod.yml, features.dev.yml) and load them in config/packages/dev/ecn_feature_toggle.yaml:
imports:
- { resource: '%kernel.project_dir%/config/features.dev.yml' }
Dependency Injection:
FeatureToggle into services/controllers:
public function __construct(private FeatureToggle $toggle) {}
Conditional Logic:
if ($toggle->isEnabled('experimental_search')) {
$results = $this->searchService->newAlgorithm();
} else {
$results = $this->searchService->legacyAlgorithm();
}
Configuration Validation:
features.yml via Symfony’s validator (extend Ecn\FeatureToggleBundle\Validator\Constraints\FeatureToggle if needed).Database-Backed Toggles (Advanced):
FeatureToggleProvider to fetch toggles from a database (e.g., Doctrine).config/services.yaml:
services:
Ecn\FeatureToggleBundle\FeatureToggle:
class: App\Service\CustomFeatureToggleProvider
Event-Driven Toggle Updates:
FeatureToggleUpdatedEvent) to invalidate caches or log changes.Testing:
FeatureToggle in tests:
$this->mock(FeatureToggle::class)->shouldReceive('isEnabled')->andReturn(true);
Twig Extensions:
{% if 'feature' is enabled with priority('high') %}
{# Only show if feature is enabled AND priority is high #}
{% endif %}
Configuration Overrides:
features.yml overrides Symfony’s default config.imports in config/packages/ecn_feature_toggle.yaml to merge configs:
ecn_feature_toggle:
features:
%ecn_feature_toggle.features% # Preserve defaults
new_feature: true # Override/add
Caching:
features.yml require a restart.Twig Dependency:
twig/twig if using Twig features.Namespace Collisions:
App or Service, it may conflict with Symfony’s container.app_new_ui).Validation Errors:
symfony/validator or use yaml.lint:
composer require --dev symfony/validator
Log Toggle States:
$toggle->enableDebug(true); // If supported (check bundle docs)
\Log::debug('Feature "dark_mode" enabled:', ['status' => $toggle->isEnabled('dark_mode')]);
Check Loaded Features:
dump($toggle->getAllFeatures());
Environment-Specific Issues:
features.yml is loaded per environment by checking:
$this->container->getParameter('ecn_feature_toggle.features');
Custom Providers:
Ecn\FeatureToggleBundle\Provider\FeatureToggleProviderInterface for non-YAML sources (e.g., Redis, API).Event Listeners:
FeatureToggleEvents (if the bundle dispatches them) to react to toggle changes.Twig Extensions:
$twig->addFunction(new \Twig\TwigFunction('feature_priority', function ($name, $priority) use ($toggle) {
return $toggle->isEnabled($name) && $toggle->getPriority($name) >= $priority;
}));
Validation Constraints:
features.yml:
ecn_feature_toggle:
features:
new_ui:
enabled: false
constraints:
- ValidFeatureName: ~
How can I help you explore Laravel packages today?