Parsing YAML files in Laravel 5
Parsing YAML files in Laravel 5

Parsing YAML files in Laravel 5

I decided to make this small guide for those in search of YAML support in Laravel 5, since most of the articles online describe outdated processes, using older Laravel versions or the need to install extensions.

Laravel 5 is a modern PHP web framework, one of the most popular ones, alongside Symfony, Codeigniter, Yii and others. Its creators are famous for their openness towards employing good solutions instead of reinventing the wheel, so Laravel is easily extensible and friendly to external component integration.

So, without further ado, here is how you get YML file support in Laravel 5:

  1. Install the Symfony YAML Component using composer:
composer require symfony/yaml

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

‘Yaml’ => ‘SymfonyComponentYamlYaml’

3. In your desired controller/service:

use Yaml;
[ … ]
$yamlContents = Yaml::parse(file_get_contents(‘filepath’));

And you’re done! Far simpler than any of the old solutions. This example covers only YAML parsing, but feel free to use any of the features documented in the Symfony YAML Component Documentation.