Symfony - konfiguracja połączenia z bazą danych

Autor podstrony: Krzysztof Zajączkowski

Stronę tą wyświetlono już: 2852 razy

Cofnijmy się nieco w czasie i przestrzeni do początków przygody z Symfony. Okazuje się bowiem, że już na początkowym etapie tworzenia projektu strony w Symfony wyświetlane są podstawowe informacje na temat miejsca, gdzie framework ten ma zapisane ustawienia. Warto więc sobie przypomnieć, jak wyglądał komunikat wygenerowany przy tworzeniu projektu:

              
 What's next? 
              

  * Run your application:
    1. Change to the project directory
    2. Create your code repository with the git init command
    3. Run composer require server --dev to install the development web server,
       or configure another supported web server https://symfony.com/doc/current/setup/web_server_configuration.html

  * Read the documentation at https://symfony.com/doc

                        
 Database Configuration 
                        

  * Modify your DATABASE_URL config in .env

  * Configure the driver (mysql) and
    server_version (5.7) in config/packages/doctrine.yaml

              
 How to test? 
              

  * Write test cases in the tests/ folder
  * Run php bin/phpunit

Jak widać, konfiguracja została przeniesiona w Symfony 4.3 do pliku .env. Z tego też względu z najdzikszą rozkoszą przedstawiam poniżej zawartość tegoż pliku:

# In all environments, the following files are loaded if they exist, # the later taking precedence over the former: # # * .env contains default values for the environment variables needed by the app # * .env.local uncommitted file with local overrides # * .env.$APP_ENV committed environment-specific defaults # * .env.$APP_ENV.local uncommitted environment-specific overrides # # Real environment variables win over .env files. # # DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES. # # Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2). # https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration ###> symfony/framework-bundle ### APP_ENV=dev APP_SECRET=135d01e162bb1396c87667c6896e7aa4 #TRUSTED_PROXIES=127.0.0.1,127.0.0.2 #TRUSTED_HOSTS='^localhost|example.com$' ###< symfony/framework-bundle ### ###> doctrine/doctrine-bundle ### # Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url # For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db" # Configure your db driver and server_version in config/packages/doctrine.yaml DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name ###< doctrine/doctrine-bundle ### ###> symfony/swiftmailer-bundle ### # For Gmail as a transport, use: "gmail://username:password@localhost" # For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode=" # Delivery is disabled by default via "null://localhost" MAILER_URL=null://localhost ###< symfony/swiftmailer-bundle ###

Oczywiście w tym przypadku interesuje mnie linijka 27:

DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name

w której:

Propozycje książek