syzygypl/kunstmaan-feature-switches-bundle

1.0.2 2018-08-07 11:25 UTC

This package is not auto-updated.

Last update: 2024-09-23 07:21:30 UTC


README

步骤 1: 下载包

打开命令行,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本

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

此命令要求您全局安装 Composer,请参阅 Composer 文档中的安装章节

步骤 2: 启用包

然后,通过将其添加到项目 app/AppKernel.php 文件中注册的包列表中,启用该包

<?php
// app/AppKernel.php

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

        // ...
    }

    // ...
}

步骤 3: 生成数据库模式

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

步骤 4: 导入路由

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

用法

创建一个新的开关

只需进入 /{locale}/admin/settings,然后在侧边栏菜单中选择“特征开关”。然后使用右上角的大蓝色按钮。名称和代码必须唯一。

Twig

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

PHP

在控制器中

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

最佳实践

为所有开关创建一个常量集合

创建一个新文件:app/Features.php

// app/Features.php

<?php

class Features
{
    const MY_SPECIAL = 'my_special_feature';
}

在自动加载中引入刚创建的文件

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

现在您可以在 twig 中使用常量

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