相关疑难解决方法(0)

如何从Symfony2中的服务中解析yaml文件

我想从我的一个服务中的yaml文件中获取一个数组,我对如何注入要在我的services.yml中使用的文件感到困惑.

# /path/to/app/src/Bundle/Resources/config/services.yml
parameters:
    do_something: Bundle\DoSomething
    yaml.parser.class: Symfony\Component\Yaml\Parser
    yaml.config_file: "/Resources/config/config.yml" # what do I put here to win!

services:
    yaml_parser:
        class: %yaml.parser.class%

    do_parsing:
        class: %do_something%
        arguments: [ @yaml_parser, %yaml.config_file% ]
Run Code Online (Sandbox Code Playgroud)

在我的服务中我有

# /path/to/app/src/Bundle/DoSomething.php

<?php

namespace Bundle;

use \Symfony\Component\Yaml\Parser;

class DoSemething
{
    protected $parser;
    protected $parsed_yaml_file;

    public function __construct(Parser $parser, $file_path)
    {
       $this->parsed_yaml_file = $parser->parse(file_get_contents(__DIR__ . $file_path));
    }

    public function useParsedFile()
    {
        foreach($parsed_yaml_file as $k => $v)
        {
            // ...  etc etc 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这可能是完全错误的方法,如果我应该做其他事情请告诉我!

yaml symfony

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

symfony ×1

yaml ×1