【问题标题】:Dynamic database parameters file on SymfonySymfony 上的动态数据库参数文件
【发布时间】:2017-05-19 00:24:17
【问题描述】:

我是 Symfony 的新手,我正在尝试根据主机加载不同的 parameters.yml,包括数据库配置。因此,在 apache vhosts 中,我定义了一个变量,通过 $_SERVER["CLIENTID"] (指令 SetEnv)让我知道 PHP 中的客户端是什么。现在我想,例如,如果 CLIENTID 是“client1”,则加载 parameters_client1.yml,其中包含 db_host、db_user 等... 我试过这种方式:

app/config/config.yml

doctrine:
    dbal:
        host:     "%db_host%"
        dbname:   "%db_name%"
        user:     "%db_user%"
        password: "%db_password%"

app/config/parameters_client1.yml

parameters:
    db_host: "localhost"
    db_name: "client1_database"
    db_user: "client1"
    db_password: "something"

src/AppBundle/DependencyInjection/Configuration.php

namespace AppBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('appbundle');
        return $treeBuilder;
    }
}

src/AppBundle/DependencyInjection/AppExtension.php

namespace AppBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class AppExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        $rootdir = $container->getParameter('kernel.root_dir');
        // Load the bundle's services.yml
        $loader = new Loader\YamlFileLoader($container, new         FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
        // Load parameters depending on current host
        $paramLoader = new Loader\YamlFileLoader($container, new FileLocator($rootdir.'/config')); // Access the root config directory

        $parameters = "parameters.yml";

        if( array_key_exists("CLIENTID", $_SERVER) )
        {
            $paramfile = sprintf('parameters_%s.yml', $_SERVER["CLIENTID"]);
            if (file_exists($rootdir.'/config/'.$paramfile))
            {
                $parameters = $paramfile;
            }
        }

        $paramLoader->load($parameters);
    }
}

但这不起作用,我收到以下错误:

ParameterNotFoundException in ParameterBag.php line 100:
You have requested a non-existent parameter "db_host".

请有人解释一下我错过了什么? 我正在使用 Symfony 3.2

【问题讨论】:

    标签: symfony


    【解决方案1】:

    粗略的猜测是你在 app/config/config.yml 中包含了资源

    imports:
        - { resource: parameters.yml }
        - { resource: security.yml }
        - { resource: services.yml }
    

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 2019-01-19
      相关资源
      最近更新 更多