php - Using the resolve operator in the Symfony config env() helper

21

This article introduces type-casting and some convenient operators which can be used inside theenv() helper in the Symfony configs. Everything's clear except theresolve: operator. The article says:

The resolve: operator replaces container parameter names by their values:

What I am going to have the parameters whose names are taken from the values of the env variables? What's the point?

It's used in the doctrine bundle's config, for example:

dbal:
    # configure these for your database server
    driver: 'pdo_mysql'
    server_version: '5.7'
    charset: utf8mb4

    # With Symfony 3.3, remove the `resolve:` prefix
    url: '%env(resolve:DATABASE_URL)%'

I was googling the issue, but it's almost no info on the Internet, and it doesn't clarify anything to me.

873

Answer

Solution:

It's pretty simple. In your example you have:

url: '%env(resolve:DATABASE_URL)%'

If DATABASE_URL value itself contains any container parameters, like Romain example:

parameters:
    env(DATABASE_URL): 'sqlite://%kernel.project_dir%/var/data.db'
    db_dsn: '%env(resolve:DB)%'

Since DATABASE_URL contains %kernel.project_dir% parameter, which would be the root directory of your project.

By using resolve, you make this %kernel.project_dir% parameter being replaced by it's value.

Without resolve url will be:

url: 'sqlite://%kernel.project_dir%/var/data.db'

With resolve (an example) url will be:

url: 'sqlite:///Users/your_name/whatever/directory/var/data.db'
92

Answer

Solution:

What's the actual value ofDATABASE_URL and what's the expected result ?

Example from documentation:

parameters:
    project_dir: '/foo/bar'
    env(DB): 'sqlite://%%project_dir%%/var/data.db'
    db_dsn: '%env(resolve:DB)%'

People are also looking for solutions to the problem: php - SYMFONY 3 Twig conditions route

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.