itf/yaml-config-bundle

该软件包的官方仓库似乎已不存在,因此该软件包已被冻结。

安装: 256

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

语言:JavaScript

0.1 2016-06-09 12:37 UTC

This package is not auto-updated.

Last update: 2024-02-03 15:04:25 UTC


README

一个简单的配置包,用于设置快速基于配置的条件代码,例如以下内容

<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction()
    {
        if ($this->get('itf.yconf')->get('some.configuration') == true) {
            // do stuff
        } 
    }
}

使用此示例配置文件

some:
    configuration: 1
    color: #fff
    text: 'Lorem ipsum'

安装

composer require itf/yaml-config-bundle "dev-master"

AppKernel.php文件中激活该包

public function registerBundles() 
{
    $bundles = array(
        //[...]
        new \ITF\YamlConfigBundle\YamlConfigBundle()
    );
    // ...
}    

然后,将以下内容添加到您的config.yml文件中

imports:
    - { resource: @YamlConfigBundle/Resources/config/services.yml }

#... 
    
yaml_config:
    file_path: "%kernel.root_dir%/config/some_config_file.yml"

提示:不要插入全局config.yml文件的路径,而是创建一个新的文件。如果该文件不存在,它将自动创建。

最后一步是按照以下方式将路由添加到您的routing.yml文件中

yaml_config:
    resource: "@YamlConfigBundle/Resources/config/routing.yml"
    prefix:   /_config

最后,安装资产

php app/console assets:install

使用

您现在可以在任何需要的地方使用itf.yconf服务。例如,在控制器(如上面的示例)或甚至twig模板中。

此包使用symfony的PropertyAccess组件来访问配置文件。

您可以选择使用文本编辑器或Web-GUI编辑配置文件。打开http://localhost:8000/_configweb gui

在twig模板中使用

为了在twig模板中访问此服务,您需要将其添加到其全局变量中。在您的config.yml中添加以下内容

twig:
    globals:
       yconf: "@itf.yconf"

现在您可以像这样轻松访问它

{% if yconf.get('some.configuration') is sameas(true) %}
    <div style="background: {{ yconf.get('some.color') }};">
        {{ yconf.get('some.text') }}
    </div>
{% endif %}

故障排除

为什么我的更改没有保存?请确保您的配置文件具有适当的访问权限。使用chmod 644 [path to config file]应该可以解决这个问题。

许可证

MIT