Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7045095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:29:17+00:00 2026-05-28T02:29:17+00:00

I want to get an array from a yaml file inside one of my

  • 0

I want to get an array from a yaml file inside one of my services, and I am a little confused of how to inject the file to use in my 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% ]

In my service I have

# /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 
        }
    }
}

This may be the completely wrong approach, if I should be doing something else please let me know!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:29:18+00:00Added an answer on May 28, 2026 at 2:29 am

    First I’ll explain why I implemented my solution for you to decide if this case is right for you.

    I needed a way to easily load custom .yml files in my bundle (for lots of bundles) so adding a separate line to app/config.yml for every file seemed like a lot of hassle for every setup.

    Also I wanted most of the configs to be already loaded by default so end-user wouldn’t even need to worry about configuring most of the time, especially not checking that every config file is setup correctly.

    If this seems like a similar case for you, read on. If not, just use Kris solution, is a good one too!


    Back when I encountered a need for this feature, Symfony2 didnt’t provide a simple way to achieve this, so here how I solved it:

    First I created a local YamlFileLoader class which was basically a dumbed down Symfony2 one:

    <?php
    
    namespace Acme\DemoBundle\Loader;
    
    use Symfony\Component\Yaml\Yaml;
    use Symfony\Component\Config\Loader\FileLoader;
    
    /**
     * YamlFileLoader loads Yaml routing files.
     */
    class YamlFileLoader extends FileLoader
    {
        /**
         * Loads a Yaml file.
         *
         * @param string $file A Yaml file path
         *
         * @return array
         *
         * @throws \InvalidArgumentException When config can't be parsed
         */
        public function load($file, $type = null)
        {
            $path = $this->locator->locate($file);
    
            $config = Yaml::parse($path);
    
            // empty file
            if (null === $config) {
                $config = array();
            }
    
            // not an array
            if (!is_array($config)) {
                throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $file));
            }
    
            return $config;
        }
    
        /**
         * Returns true if this class supports the given resource.
         *
         * @param mixed  $resource A resource
         * @param string $type     The resource type
         *
         * @return Boolean True if this class supports the given resource, false otherwise
         *
         * @api
         */
        public function supports($resource, $type = null)
        {
            return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type);
        }
    }
    

    Then I updated DIC Extension for my bundle (it’s usually generated automatically if you let Symfony2 create full bundle architecture, if not just create a DependencyInjection/<Vendor&BundleName>Extension.php file in your bundle directory with following content:

    <?php
    
    namespace Acme\DemoBundle\DependencyInjection;
    
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\Config\FileLocator;
    use Symfony\Component\HttpKernel\DependencyInjection\Extension;
    use Symfony\Component\DependencyInjection\Loader;
    
    use Acme\DemoBundle\Loader\YamlFileLoader;
    
    /**
     * This is the class that loads and manages your bundle configuration
     *
     * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
     */
    class AcmeDemoExtension extends Extension
    {
        /**
         * {@inheritDoc}
         */
        public function load(array $configs, ContainerBuilder $container)
        {
            $configuration = new Configuration();
            $config = $this->processConfiguration($configuration, $configs);
    
            $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
    
            $loader->load('services.xml');
    
            // until here everything is default config (for your DIC services)
    
            $ymlLoader = new YamlFileLoader(new FileLocator(__DIR__.'/../Resources/config'));
            $container->setParameter('param_name', $ymlLoader->load('yaml_file_name'))); // load yml file contents as an array
        }
    }
    

    And now you can access/pass your yaml config as simple service parameter (i.e. %param_name% for services.yml)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get an array of strings reading from arrays.xml file we add
I want to get some objects from array contains objects where one of objects
I want to get the value from an array thats in a while fetchrow..
I want to get three highes values from my array, but it should be
I just want to quickly store an array which I get from a remote
I aam trying to GET an array from a JSON file using JQuery's ajax
I want an array that gets data from one class to be avaliable to
how can i get array value from php to javascript, and use the value
Okay, I am a noob and want to get a array from my server
I want to get the internal byte array from ByteArrayInputStream. I don't want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.