10

In the project I'm working on, it's a CMS, and within it I manage brand website themes. My question is I need to read the header of the file, how do I do this inside the controller?

I need somehow to read this file and list the titles of headers or footers that exist in it.

  • header:
  • header_submenu_menu
  • header_submenu_models
  • footer_mobile:
  • footer_desktop:
  • footer_socialmedia:

I'm using this package: https://github.com/antonioribeiro/yaml

menu.yaml

1
  • Is the package you mentioned in your question not working?? What have you tried so far?
    – Rwd
    Jan 10, 2019 at 13:17

1 Answer 1

25

There's a great article about YAML here.

To excerpt from the page, you only need to follow a few simple steps:

1 Install the Symfony YAML Component using composer:

composer require symfony/yaml

2 In config/app.php under aliases, add the following entry:

'Yaml' => 'Symfony\Component\Yaml\Yaml'

3 In your desired controller/service:

use Yaml;
$yamlContents = Yaml::parse(file_get_contents('filepath'));

And that's it.

This solution works fairly universally, relying on PHP's composer library for you to include the library, then simply calling parse methods to unmarshal your YAML into a usable PHP variable. For the example shown here, try something like:

print_r($yamlContents)

To see how to use the data now that it's been converted.

15
  • I've seen that one. Jan 10, 2019 at 13:03
  • Then what's the specific issue you're having? That article is exactly what any of us could explain. Install the package you want to use with composer, and then use it via an Alias.
    – syntaqx
    Jan 10, 2019 at 13:04
  • In case I need to list the titles, this file, being that I do not have much experience with laravel with yaml, I'm kind of lost. This data will be listed in a view. Jan 10, 2019 at 13:12
  • 2
    you will be downvoted for posting an answer with only a link to an external resource... WHile this can solve the problem in the answer it's better to include the main part of the linked resource here, because in the future this resource can go offline and become unuseful... so please improve your answer
    – DaFois
    Jan 10, 2019 at 13:22
  • 1
    @DaFois I was unaware this was the convention. I've updated my answer.
    – syntaqx
    Jan 10, 2019 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.