11

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? Commented Jan 10, 2019 at 13:17

1 Answer 1

27

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.

Sign up to request clarification or add additional context in comments.

16 Comments

I've seen that one.
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.
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.
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 I was unaware this was the convention. I've updated my answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.