Beginner
How do you handle environment variables in a Symfony application?
Environment variables in Symfony allow you to configure application settings based on the environment (development, production, etc.). This is crucial for managing sensitive data, like API keys or database credentials.
To handle environment variables, follow these steps:
- Create a .env File: Define your environment variables in a .env file located in the project's root directory.
- Access Variables: Use the `getenv()` function or the Symfony `$_ENV` superglobal to access these variables in your code.
- Configure Services: Inject these variables into services using parameter configuration.
Example of a .env file:
DATABASE_URL=mysql://user:password@localhost:3306/db_name