Environment Configuration (.env)
Relevant source files
The following files were used as context for generating this wiki page:
Environment configuration in this project is managed through the use of environment variables, typically defined in a .env file at the root of the application. This system allows the application to adapt its behavior across different execution contexts—such as local development, testing, and production—without modifying the core codebase.
The framework utilizes the env() helper function to retrieve these values within configuration files. This function often provides a default fallback value if the specific environment variable is not defined, ensuring the application remains functional under various setups.
Sources: config/app.php:14-25, composer.json:44-49
Core Application Settings
The primary identity and operational mode of the application are dictated by variables prefixed with APP_. These settings control the application name, deployment environment, and security features.
- APP_NAME: Defines the application's name used in UI elements and notifications.
- APP_ENV: Determines the current environment (e.g.,
production,local,testing). - APP_DEBUG: A boolean toggle that enables detailed error messages and stack traces.
- APP_KEY: A critical 32-character string used for encryption services.
The following diagram illustrates the flow of environment variables into the application's core configuration:
The diagram shows how the .env file serves as the source of truth for high-level application behavior.
Sources: config/app.php:14-106
Database and Persistence Configuration
Database connections are highly dependent on environment variables to manage credentials and connection strings. The system supports multiple drivers including SQLite, MySQL, MariaDB, PostgreSQL, and SQL Server.
Connection Parameters
Common variables used across database drivers include:
- DB_CONNECTION: Specifies the default driver (defaults to
sqlite). - DB_HOST, DB_PORT: Network location of the database server.
- DB_DATABASE: The name of the database or the path to the SQLite file.
- DB_USERNAME, DB_PASSWORD: Credentials for authentication.
Redis Configuration
Redis integration also relies on environment variables for clustering, prefixing, and connection details like REDIS_HOST and REDIS_PORT.
| Variable | Usage | Default Fallback |
|---|---|---|
DB_CONNECTION | Default database driver | sqlite |
DB_DATABASE | Database name/path | database_path('database.sqlite') |
DB_SSLMODE | PostgreSQL SSL mode | prefer |
REDIS_CLIENT | Client library for Redis | phpredis |
Sources: config/database.php:19-158, CHANGELOG.md:14-15
Infrastructure and Service Drivers
The application uses environment variables to swap between different implementation "drivers" for various subsystems. This is particularly useful for testing, where external services are often replaced with local or "null" alternatives.
Session and Cache
Sessions are configured via SESSION_DRIVER, supporting options like database, redis, file, and cookie. In testing environments, these are often overridden to array to avoid persistence between tests.
Filesystems and Storage
The FILESYSTEM_DISK variable determines where files are stored.
- Local: Uses
storage_path('app/private'). - Public: Uses
storage_path('app/public')and relies onAPP_URLto generate accessible links. - S3: Requires
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY, andAWS_BUCKET.
The diagram represents the relationship between environment variables and storage driver selection.
Sources: config/session.php:19-25, config/filesystems.php:19-54, config/app.php:126-130
Testing Environment Overrides
When running tests via PHPUnit, the application behavior is modified through specific environment variables defined in the phpunit.xml file. These settings ensure the test suite runs in a controlled, isolated state, typically using in-memory databases and synchronous execution.
| Testing Variable | Value | Purpose |
|---|---|---|
APP_ENV | testing | Switches app to test mode |
DB_CONNECTION | sqlite | Uses SQLite for tests |
DB_DATABASE | :memory: | Ensures database exists only in RAM |
QUEUE_CONNECTION | sync | Processes jobs immediately |
CACHE_STORE | array | Non-persistent cache for tests |
Sources: phpunit.xml:27-41
Setup and Initialization
The project includes automation scripts in composer.json to manage the lifecycle of environment files.
- Creation: During installation, the system checks if a
.envfile exists. If not, it copies.env.exampleto.env. - Key Generation: The
php artisan key:generatecommand is triggered to populate theAPP_KEYvariable, ensuring the application has a unique encryption secret. - Migration: The environment is used to establish a database connection, allowing
php artisan migrateto create the necessary tables such asusers,sessions, andjobs.
Sources: composer.json:44-67, database/migrations/0001_01_01_000000_create_users_table.php:7-25, database/migrations/0001_01_01_000002_create_jobs_table.php:11-30
Conclusion
The Environment Configuration system serves as the foundational bridge between the application code and the infrastructure it runs on. By abstracting sensitive credentials and environment-specific logic into variables, the architecture maintains high portability and security across development, testing, and production stages.