1
我是Symfony的新手,我試圖根據主機加載不同的parameters.yml,包括數據庫配置。所以,在apache虛擬主機中,我定義了一個變量,讓我知道$ _SERVER [「CLIENTID」](指令SetEnv)在PHP中的客戶端。現在我想,例如,如果客戶端ID是「客戶端1」,載parameters_client1.yml,包含DB_HOST,DB_USER,等... 我試着這樣說:Symfony上的動態數據庫參數文件
應用程序/配置/ config.yml
doctrine:
dbal:
host: "%db_host%"
dbname: "%db_name%"
user: "%db_user%"
password: "%db_password%"
應用/配置/ 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