doubi9999 2017-01-19 13:15
浏览 23
已采纳

如何获取symfony控制器内的服务参数?

Say I have a app/config/parameters.yml which contains

parameters:
    database_driver:   pdo_mysql
    ...
nacho_image: 
    upload: true
    progress: false 
    key: secret_id

Now, I would like to get upload, progress, key in my controller, but doing

$this->get('upload'); 
$this->get('nacho_image.upload'); 
$this->get('nacho.image.upload'); 
$this->getParameter('nacho.upload'); 
$this->getParameter('acho_image.upload.upload'); 

doesn't work...

  • 写回答

2条回答 默认 最新

  • dsdf64562672 2017-01-19 13:20
    关注

    You cannot directly get a nested parameter. you have to get the parent key and you will get all the content as an array

    $nachoImageConfig = $this->getParameter('nacho_image');
    $nachoImageUpload = $nachoImageConfig['upload'];
    

    From the doc (http://symfony.com/doc/current/service_container/parameters.html):

    The used . notation is just a Symfony convention to make parameters easier to read. Parameters are just flat key-value elements, they can't be organized into a nested array

    That is why in most config files, the entries under 'parameters:' are fully qualified like this:

    parameters:
    
        ...
    
        nacho_image.upload: true
        nacho_image.progress: false 
        nacho_image.key: secret_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?