Get parameters.yml parameters in AppKernel.php

Does anyone know how I can get .yml (or _dev) options in AppKernel.php?

I want to dynamically change getLogDir () ('mylogsdir') variables?

AppKernel.php:

$this->rootDir.'/'.$this->environment.'/'.$myLogDir;

parameters.yml:

parameters:
    myLogDir: 'logdir'

Is it possible?

Thanks a lot Fabris

+4
source share
2 answers

You can try something like this:

In 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;
    }
+3
source

I have a solution, here in vhost under nginx:

 fastcgi_param SYMFONY__KERNEL__LOGS_DIR "/path/to/logs";
 fastcgi_param SYMFONY__KERNEL__CACHE_DIR "/path/to/cache";

And in the AppKernel.php file:

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();
    }
}

thank,

Fabris

+1
source

All Articles