Environment variables

OXID eShop supports loading environment variables via a .env file.

This feature simplifies the management of sensitive configuration values and environment-specific settings.

Feature Overview

Define environment variables in a .env file located in the root directory of your project.

These variables can then be accessed using:

  • The getenv() PHP function

  • Injection into container parameters

Using environment variables

  1. Create a .env file in the root directory of your project. Define your environment variables in the following format:

    # .env
    OXID_ENV=production
    DATABASE_URL=mysql://user:[email protected]:3306/db_name
    API_KEY=your_api_key_here
    
  2. Access the loaded environment variables in one of the following two ways:

    • Use getenv() in your PHP code:

      <?php
      
      $environment = getenv('OXID_ENV');
      echo "Current environment: $environment";
      
    • Define the environment variables in your services.yaml configuration file to be used in services:

      parameters:
          app.env: '%env(OXID_ENV)%'
          database.url: '%env(DATABASE_URL)%'
          api.key: '%env(API_KEY)%'
      
      services:
          App\Service\SomeService:
              arguments:
                  $env: '%app.env%'
                  $dbUrl: '%database.url%'
      

Implementing Best Practices

  • To prevent sensitive data from being pushed to version control, do not commit the .env file. Instead, add it to your .gitignore file.

  • To help other developers set up their local environment, provide a .env.dist file with default values.