Symfony获取嵌套4次的值

4ioopgfo  于 2022-11-25  发布在  其他
关注(0)|答案(1)|浏览(130)

我的参数是这样构造的

parameters:
    Adress:
        Shops:
            Europe: Road1
            Asia: Road2
        Headquarters:
            Europe: Streetname1
            Asia: Streetname2

如果我想在PHP中获得一个参数,该如何工作呢?
这应该会返回Road 1,对吗?

$this->getParameter('adress');

parameter:
    adress: Road1

还有这个

$this->getParameter('adress')['shop'];

parameter:
    adress:
        shop: Road1

如何从我在开始展示的示例中获取参数?
答:
如果您想查询一个新的参数,请输入以下参数:
谢谢Toskan!
提前感谢你的帮助。

i5desfxk

i5desfxk1#

symfony区分大小写
调试参数
php bin/console debug:container --parameters
您会发现Adress
安装psysh以交互方式使用它

$ php bin/console psysh
Psy Shell v0.11.8 (PHP 8.1.2 — cli) by Justin Hileman
>>> $container->getParameter('Adress')
=> [
     "Shops" => [
       "Europe" => "Road1",
       "Asia" => "Road2",
     ],
     "Headquarters" => [
       "Europe" => "Streetname1",
       "Asia" => "Streetname2",
     ],
   ]

>>> $container->getParameter('Adress.Shop')
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException with message 'The parameter "Adress.Shop" must be defined.'
>>> $container->getParameter('adress')
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException with message 'The parameter "adress" must be defined.'
>>> $container->getParameter('Adress')
=> [
     "Shops" => [
       "Europe" => "Road1",
       "Asia" => "Road2",
     ],
     "Headquarters" => [
       "Europe" => "Streetname1",
       "Asia" => "Streetname2",
     ],
   ]
>>> $container->getParameter('Adress')['Shops']
=> [
     "Europe" => "Road1",
     "Asia" => "Road2",
   ]

>>> $container->getParameter('Adress')['Shops']['Europe']
=> "Road1"

因此工作结果为$container->getParameter('Adress')['Shops']['Europe']
一定要接受这个答案

相关问题