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条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助