有谁知道如何在AppKernel.php中获取parameters.yml(或_dev)?
我想动态更改getLogDir()变量(' mylogsdir')?
AppKernel.php:
$this->rootDir.'/'.$this->environment.'/'.$myLogDir;
parameters.yml:
parameters:
myLogDir: 'logdir'
有可能吗?
非常感谢 和Fabrice
答案 0 :(得分:3)
您可以尝试这样的事情:
在AppKernel.php中:
...
use Symfony\Component\Yaml\Yaml;
...
class AppKernel extends Kernel
{
....
public function getLogDir()
{
$array = Yaml::parse(file_get_contents($this->getRootDir().'/config/parameters.yml'));
// or
// $this->getRootDir().'/config/parameters_'.$this->getEnvironment().'.yml'
$myLogDir = $array['parameters']['myLogDir'];
return $this->rootDir.'/'.$this->environment.'/'.$myLogDir;
}
答案 1 :(得分:1)
我的解决方案,在nginx下的vhost中:
fastcgi_param SYMFONY__KERNEL__LOGS_DIR "/path/to/logs";
fastcgi_param SYMFONY__KERNEL__CACHE_DIR "/path/to/cache";
并且,在AppKernel.php文件中:
public function getCacheDir()
{
if (!empty($this->getEnvParameters()['kernel.cache_dir'])) {
return $this->getEnvParameters()['kernel.cache_dir'].'/'.$this->environment;
} else {
return parent::getCacheDir();
}
}
public function getLogDir()
{
if (!empty($this->getEnvParameters()['kernel.logs_dir'])) {
return $this->getEnvParameters()['kernel.logs_dir'].'/'.$this->environment;
} else {
return parent::getLogDir();
}
}
感谢,
和Fabrice