Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read yaml files in laravel?

Tags:

yaml

laravel

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

like image 722
Romulo Sousa Avatar asked Jan 10 '19 13:01

Romulo Sousa


1 Answers

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.

like image 94
syntaqx Avatar answered Oct 12 '22 02:10

syntaqx