Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the parameters.yml params in the AppKernel.php

Can anybody knows how can I get the parameters.yml (or _dev) in the AppKernel.php ?

I want to change the getLogDir() variables('mylogsdir') dynamicly ?

AppKernel.php :

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

parameters.yml:

parameters:
    myLogDir: 'logdir'

Is it possible ?

Thanks a lot Fabrice

like image 805
fabrice Avatar asked Jun 28 '16 08:06

fabrice


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;
    }
like image 96
doydoy44 Avatar answered Nov 14 '22 09:11

doydoy44


I've the solution, here in the 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();
    }
}

thanks,

Fabrice

like image 39
fabrice Avatar answered Nov 14 '22 10:11

fabrice