PHPackages                             thamtech/yii2-yaml - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. thamtech/yii2-yaml

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

thamtech/yii2-yaml
==================

A Yaml helper for Yii2

v1.0.0(4y ago)16.9k—0%11BSD-3-ClausePHPPHP &gt;=7.2.5

Since Jun 14Pushed 4y ago1 watchersCompare

[ Source](https://github.com/thamtech/yii2-yaml)[ Packagist](https://packagist.org/packages/thamtech/yii2-yaml)[ Fund](https://liberapay.com/thamtech)[ RSS](/packages/thamtech-yii2-yaml/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (3)Versions (8)Used By (1)

Yii2 Yaml
=========

[](#yii2-yaml)

Yii2 Yaml provides a Yaml helper and a Yaml parser/dumper extension.

The Yaml helper provdes`encode()`, `decode()` and `errorSummary()`similar to [yii\\helpers\\Json](https://www.yiiframework.com/doc/api/2.0/yii-helpers-json).

Yii2 Yaml also includes an extension of the [symfony/yaml](https://github.com/symfony/yaml) library that supports custom yaml tags representing [UnsetArrayValue](https://www.yiiframework.com/doc/api/2.0/yii-helpers-unsetarrayvalue)and [ReplaceArrayValue](https://www.yiiframework.com/doc/api/2.0/yii-helpers-replacearrayvalue), as well as allowing you to attach events to handle any other custom yaml tags.

For license information check the [LICENSE](LICENSE.md)-file.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

```
php composer.phar require --prefer-dist thamtech/yii2-yaml

```

or add

```
"thamtech/yii2-yaml": "*"

```

to the `require` section of your `composer.json` file.

Background
----------

[](#background)

The goal of this Yii2 Yaml extension is to support Yaml in Yii2 the way support for JSON is built-in. A `Yaml` helper is introduced to match the API of Yii2's built-in Json helper.

Furthermore, we have extended the `symfony/yaml` library to support decoding and encoding of `ReplaceArrayValue` and `UnsetArrayValue` objects for use with Yii's [ArrayHelper::merge()](https://www.yiiframework.com/doc/api/2.0/yii-helpers-basearrayhelper#merge%28%29-detail)method. You can use Yii2 event handlers to process custom tags as they are parsed/decoded and pre-process objects before they are dumped/encoded.

Yii2 uses encode/decode terminology in the Json helper, while `symfony/yaml`uses dump/parse terminology. Our helper is consistent with the Json helper in using the encode/decode terminology.

Usage
-----

[](#usage)

### Decoding/Parsing

[](#decodingparsing)

Example Yaml:

```
people:
    john:
        id: 1
        name: John
    # A value must be associated with a tag: either a block value (indented
    # section under the key) or an inline value.
    # Here, we demsonstrate a block value following the !yii/helpers/ReplaceArrayValue/tag:
    bob: !yii/helpers/ReplaceArrayValue
        id: 1001
        name: Bob
    # The `unsetArrayValue` handler requires that the associated value be
    # empty, so use an inline `{}`, `[]`, `false`, or `null` value.
    jane: !yii/helpers/UnsetArrayValue null
    susan: !lookupIdFromEmployeeNumber
        employee_number: 1234
        name: Susan
```

Example decoding using the Yaml helper:

```
