https://a.storyblok.com/f/270183/1368x665/83ad497503/25apr_dev-blog_enviormental-variables_sympony.jpg

How to Use PHP Environment Variables in Symfony

最終更新日 April 17, 2025

Introduction

Environment variables (env vars) allow you to configure your Symfony application based on the execution environment. This tutorial will guide you through using environment variables in a Symfony project.

Understanding Environment Variables and Parameters

Symfony uses environment variables to store configuration values such as database credentials, API keys, and other sensitive or environment-specific information. These values can be set in a .env file or directly in the server environment. While environment variables define values at the system or application level, parameters are stored in the service container and can reference environment variables.

Setting Up Environment Variables

Using the .env File

Symfony projects come with a .env file where you can define environment variables:

# .env

VONAGE_API_KEY=your_api_key

VONAGE_API_SECRET=your_api_secret

You can also create an env.local file, which will override settings for your local environment.

# .env.local

VONAGE_API_KEY=your_production_api_key

VONAGE_API_SECRET=your_production_api_secret

Accessing Environment Variables in Symfony

In your .env or env.local file, there is a variable named APP_ENV. It is this setting that determines what configuration files to read in at runtime; this can be set to prod for production (for instance, if you are using a Platform-as-a-Service such as Platform.sh), test for a UAT-style environment when you are running tooling such as PHPUnit or PHPStan and finally dev for your local development environment. There are two different methods by which you can retrieve the loaded configuration.

In Configuration Files

Symfony allows referencing environment variables in configuration files using the %env(VAR_NAME)% syntax. For example, in config/packages/vonage.yaml:

Vonage:

    api_key: '%env(VONAGE_API_KEY)%'

    api_secret: '%env(VONAGE_API_SECRET)%'

In Services

You can inject environment variables into services by specifying them in config/services.yaml:

parameters:

    vonage.api_key: '%env(VONAGE_API_KEY)%'

    vonage.api_secret: '%env(VONAGE_API_SECRET)%'

Then, in your service class:

namespace App\Service;

use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

class VonageService

{

    private string $apiKey;

    private string $apiSecret;

    public function __construct(private 

ContainerBagInterface

 $config)

    {

       

$this->apiKey = $this->params->get('vonage.api_key');

$this->apiSecret = $this->params->get('vonage.api_secret');

    }

}

Alternatively, you can use PHP’s getenv() function:

$apiKey = getenv('VONAGE_API_KEY');

$apiSecret = getenv('VONAGE_API_SECRET');

In the Standard Library, this is essentially just a wrapper for the $_ENV[] Superglobal; however, it’s recommended to use Symfony’s configuration, which has a much more versatile set of tooling to handle environment variables.

Validating and Normalizing Environment Variables

Symfony provides an env() processor to transform and validate environment variables:

parameters:

    vonage_api_key: '%env(string:VONAGE_API_KEY)%'

    vonage_api_secret: '%env(string:VONAGE_API_SECRET)%'

When to Use Environment Variables vs Parameters

  • Use parameters for values that remain constant across environments.

  • Use environment variables for values that change per environment, such as API keys and secrets.

Optimizing Performance

To improve performance in production, dump environment variables into a compiled PHP file:

composer dump-env prod

This generates .env.local.php, allowing Symfony to load environment variables without parsing .env files at runtime.

Debugging Environment Variables and Parameters

Use the following commands to inspect current settings:

bin/console debug:container --env-vars

bin/console debug:container --parameters

Security Considerations

  • Never commit sensitive data in the .env file to version control.

  • Use .env.local for local overrides and configure production environments using actual environment variables.

  • Validate environment variables before using them.

Understanding Symfony Environment vs Server Environment

  • Symfony Environment (APP_ENV) defines the application mode (e.g., dev, prod, test).

  • Server Environment refers to the physical or virtual machine running the application.

Conclusion

With environment variables, Symfony gives you a cool way to tailor your app for any stage—dev, test, or production—without hardcoding a thing, keeping it clean and secure.

Got any questions or comments? Join our thriving Developer Community on Slack, follow us on X (formerly Twitter), or subscribe to our Developer Newsletter. Stay connected, share your progress, and keep up with the latest developer news, tips, and events!

Share:

https://a.storyblok.com/f/270183/384x384/b68093ec17/diana-pham.png
Diana PhamDeveloper Advocate

Diana is a developer advocate at Vonage. She likes eating fresh oysters.