Symfony 4 Flex Database Connection Using `Urlencode`

Symfony 4 Flex Database Connection Using `Urlencode`

I am starting a new project using Symfony 4 + Flex. At this time I'm trying to connect my new app to a MySQL database.

I'm following the Symfony Documentation, and I have already add doctrine to my dependencies :

composer require doctrine
composer require maker --dev

Then I've added the database connection information in the environment variable DATABASE_URL defined inside .env like this :

###> doctrine/doctrine-bundle ###
DATABASE_URL=mysql://myUser:[email protected]:3306/myDbName
###< doctrine/doctrine-bundle ###

At this point I am facing an issue :

DBALException

Malformed parameter "url".

I'm thinking this is caused by the fact that my MySQL password is using some special characters. The documentation is talking about it :

If the username, password or database name contain any character considered special in a URI (such as !, @, $, #), you must encode them. See RFC 3986 for the full list of reserved characters or use the urlencode function to encode them.

I don't really understand how or where to use the urlencode function because the .env file is not a PHP one (as doctrine.yaml).

Is someone have already use the urlencode function to encode a MySQL password containing special chars ?

Thanks.

EDIT

This is my doctrine.yaml file :

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    env(DATABASE_URL): ''

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8
        default_table_options:
            charset: utf8
            collate: utf8_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

I've not edited the database url parameter because it is already defined in .env. Am I right ?

7

1 Answer

try to change this:

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

to this:

url: '%env(DATABASE_URL)%'
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.