Referencing non-parameter entries in config.yml

2019-09-09 13:14发布

问题:

I have quite extensive config .yml files and I'd like to refer to various settings there:

security:
    role_hierarchy:
        ROLE_ADMIN: ROLE_USER
        ROLE_SUPER_USER: ROLE_ADMIN

easy_admin:
    entities:
        Group:
            form:
                fields:
                    - 
                      property: 'roles' 
                      type: choice
                      type_options: 
                          expanded: true
                          multiple: true
                          choices: "%security.role_hierarchy%"

Of course the last line doesn't work because %security.role_hierarchy% refers to parameters.security.role_hierarchy. Is there any valid way to reference security.role_hierarchy in easy_admin section?

回答1:

The only valid way to do that in YAML is using the standard feature anchors and aliases. Anchors (that what is going to be referenced) are indicated by &<name> and alias (one or more points where the anchor is referenced) are indicated by '*`:

security:
    role_hierarchy: &hr1
        ROLE_ADMIN: ROLE_USER
        ROLE_SUPER_USER: ROLE_ADMIN

easy_admin:
    entities:
        Group:
            form:
                fields:
                    -
                      property: 'roles'
                      type: choice
                      type_options:
                          expanded: true
                          multiple: true
                          choices: *hr1

The value for the mapping entry choices, when retrieved will be the mapping:

ROLE_ADMIN: ROLE_USER
ROLE_SUPER_USER: ROLE_ADMIN

(translated to the hash or dictionary like object of the programming language the YAML parser is written in), and is the same entity as the value for the key role_hierarchy.