2016-06-28 27 views
4

任何人都可以知道如何獲取AppKernel.php中的parameters.yml(或_dev)?獲取AppKernel.php中的parameters.yml參數

我想動態更改getLogDir()變量('mylogsdir')?

AppKernel.php:

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

parameters.yml:

parameters: 
    myLogDir: 'logdir' 

這可能嗎?

非常感謝 法布里斯

回答

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

我已經解決,在這裏下的nginx虛擬主機:

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

感謝,

法布里斯