9951 explained code solutions for 126 technologies


php-symfonyHow to connect to a database in PHP Symfony?


To connect to a database in PHP Symfony, you need to configure the database connection parameters in the config/packages/doctrine.yaml file.

# config/packages/doctrine.yaml
doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

The DATABASE_URL environment variable should be set in the .env file.

# .env
DATABASE_URL=mysql://db_user:[email protected]:3306/db_name

Code explanation

  • config/packages/doctrine.yaml: This file is used to configure the database connection parameters.
  • .env: This file is used to set the DATABASE_URL environment variable.
  • DATABASE_URL: This environment variable contains the database connection parameters.

Helpful links

Edit this code on GitHub