Skip to main content

🌍 Environment Variables

Warlock is powered by Mongez Dotenv, which comes with powerful features. Let's explore how it works.

🔧 Variables

The project includes a .env file containing predefined variables used alongside project configurations.

Here's a look at the default .env file contents:

# App Configurations
APP_NAME="ecommerce"

TIMEZONE=UTC

# Http Configurations
PORT=2025
HOST=127.0.0.1
BASE_URL=http://${HOST}:${PORT}

# Database Configurations
DB_AUTH=admin
DB_PORT=27017
DB_HOST=127.0.0.1
DB_NAME=ecommerce
DB_USERNAME=root
DB_PASSWORD=root

# Mail
MAIL_HOST=
MAIL_PORT=465
MAIL_ENCRYPTION=ssl
MAIL_SECURE=true
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_FROM_NAME=${APP_NAME}
MAIL_FROM_ADDRESS=

Let's go through each variable and see what it does.

  • APP_NAME: Sets the application name, used in the mailer and other places.
  • TIMEZONE: Sets the timezone for the application, used in the mailer and other places.
  • PORT: Sets the port for the HTTP server.
  • HOST: Sets the host for the HTTP server.
  • BASE_URL: Sets the base URL for the application, used with URL functions.
  • DB_AUTH: Sets the authentication database for the database connection.
  • DB_PORT: Sets the port for the database connection.
  • DB_HOST: Sets the host for the database connection.
  • DB_NAME: Sets the database name for the database connection.
  • DB_USERNAME: Sets the username for the database connection.
  • DB_PASSWORD: Sets the password for the database connection.
  • MAIL_HOST: Sets the mail host for the mailer.
  • MAIL_PORT: Sets the mail port for the mailer.
  • MAIL_ENCRYPTION: Sets the mail encryption for the mailer.
  • MAIL_SECURE: Sets the mail secure for the mailer.
  • MAIL_USERNAME: Sets the mail username for the mailer.
  • MAIL_PASSWORD: Sets the mail password for the mailer.
  • MAIL_FROM_NAME: Sets the mail from name for the mailer.
  • MAIL_FROM_ADDRESS: Sets the mail from address for the mailer.

📂 Environment Files

The project is crafted with a .env file, the default environment file loaded when running the project. However, things can get more complex when running the project in different environments, such as development and production.

In this case, create two files: .env and .env.production.

The .env file is loaded when running the project in the development environment, and the .env.production file is loaded when running the project in the production environment.

tip

You can use .env.development and .env.production to distinguish between environments, but it's recommended to use .env and .env.production to avoid confusion.

🔗 Shared Environment File

In some cases, variables are shared between all environments, such as the APP_NAME variable. You may want to set it once and use it in all environments.

To reduce duplicates between environment variables in development and production, create a .env.shared file, add all shared variables to it, and then add the following line to the .env and .env.production files. The file will be loaded automatically.