在我用这个解决方案的目的,也许有人需要这样的事:
在AppKernel我做了一个服务器变量与当前HTTP主机:
class AppKernel extends Kernel
{
/**
* @inheritdoc
*/
public function __construct($environment, $debug)
{
parent::__construct($environment, $debug);
if ($hostEnv = $this->getHostEnv()) {
$_SERVER['SYMFONY__HOST'] = $hostEnv;
}
}
...
private function getHostEnv()
{
if (!empty($_SERVER['HTTP_HOST'])) {
return $_SERVER['HTTP_HOST'];
}
return null;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
$loader->load(__DIR__.'/config/parameters.php');
}
}
随着SYMFONY__%
服务器变量你可以自动创建参数。
然后parameter.php
:
<?php
require_once('PlasticConfig.php');
if (!empty($container)) {
$plasticConfig = new PlasticConfig($container);
$container = $plasticConfig->load();
}
然后PlasticConfig.php
:
<?php
use \Symfony\Component\DependencyInjection\ContainerInterface;
class PlasticConfig {
/**
* @var Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function load()
{
/**
* this was defined in the constructor of AppKernel
* at console command there is no HTTP_HOST variable
*/
if (!$this->container->hasParameter('host')) {
return $this->container;
}
$host = $this->container->getParameter('host');
//Magic can be done here, important is, that you return your container instance here
return $this->container;
}
}
在我parameters.yml我有一些参数,名称host_de,HOST_EN ......我将通过遍历所有的参数,如果我找到与当前主机的匹配,那么我有参数名称的区域设置。之后,我只需要设置parameters.yml
的facebook参数,其中我有如下参数:facebook.de.app_id: XY
和facebook.en.app_id: ZS
。
显然,你需要解决的一些其他的方式,因为一个容器被编译后不能更改参数(假设这是一个不可改变的生产容器)。 – zerkms
在编译时真的没有办法获取请求对象? – ghostika
我不这么认为 - 它还不存在(因为它的建筑物严重依赖于容器) – zerkms