Appearance
.env File
A .env file is a configuration file that stores environment-specific variables. It acts as a central place for configuration settings that may differ between environments (local, staging, production).
Purpose:
- keeps sensitive information, such as API keys and database credentials, secure and out of the codebase
- allows different configurations for different environments (local, staging, production, etc.)
- makes it easy to switch between environments without code changes
Common contents:
- app settings (enable/disable debug mode, URL, etc.)
- database credentials
- mail server configuration
- API keys for external services
- environment differences for production environments
Syntax rules:
- every line contains one
KEY=VALUE
pair - Lines beginning with # are processed as comments and ignored
- Blank lines are allowed
To ensure the proper functioning of the .env file, it is important to follow specific syntax rules.
NOTE
You can expand this file with your own variables like, for example, API keys for external services such as Google Maps, DarkSky, etc.
Let's take a look at some of the most common variables in the .env file:
APP_DEBUG
Use APP_DEBUG=true
ONLY in a test environment. Set the value to false
in a production environment. Depending on the value of APP_DEBUG
, Laravel will display more or less information in case of an error.
WATCH OUT
Be aware that leaving APP_DEBUG=true
in a production environment can expose sensitive information to potential attackers!
APP_URL
Specify the URL for the project. For the test environment APP_URL=https://vinyl_shop.test/
is sufficient.
In production you will of course use your real URL like: APP_URL=https://vinylshop.be
.
DB_...
We use SQLite for the environment, but you can also use MySql or other popular databases. Look at the Laravel documentation for more information on how to switch to another database.
MAIL_...
When we set up our Laravel application, we also installed mailpit as our local mail server. If you want to use a real mail server, like Gmail, Outlook or the mail server form your hosting provider, you can change the values of the MAIL_... variables.
(See an example on how to configure your mail server on the Combell hosting page.)
Below are the mail configuration settings for the mailpit server:
php
MAIL_MAILER=smtp # change from log to smtp
MAIL_SCHEME=null
MAIL_HOST=127.0.0.1
MAIL_PORT=1025 # change from 2525 to 1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_FROM_ADDRESS="info@vinyl_shop.test" # replace "hello@example.com" with your mailadres e.g. "info@vinyl_shop.test"
MAIL_FROM_NAME="${APP_NAME}" # or your name here e.g. "John Doe"
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
WARNINGS
- If your variable contains spaces, surround it with quotes. E.g.
MAIL_FROM_NAME="John Doe"
- If you want a REAL email address to test certain functionalities, use Mailinator e.g.
john.doe@mailinator.com
.
Emails sent to Mailinator are public but temporary. They will disappear after a few hours.
Determine the Environment
- The current application environment (
local
,staging
, orproduction
) is determined by theAPP_ENV
variable in your .env file. - You may access this value via the
App::environment()
facade:
php
if (App::environment('local')) {
// The environment is local
}
if (App::environment(['local', 'staging'])) {
// The environment is either local OR staging...
}
if (App::environment('production')) {
// The environment is production...
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
- You may also use:
- The
@env()
Blade directive to conditionally display content based on the current environment - The
@production()
directive to display content only when the application is in production
- The
php
@env('local')
// The environment is local
@endenv
@env(['local', 'staging'])
// The environment is either local OR staging...
@endenv
@production
// The environment is production...
@endproduction
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Access Variable With Config('variable')
REMARKS
- There are different ways to access the variables in the application but Laravel recommends:
- Use the env() helper function ONLY if you need to access the variable in a config file (e.g. config/database.php)
- Use the App::environment() method to check the current environment (e.g. in a controller or in a view) e.g.
if (App::environment('local')) { ... }
if (App::environment(['local', 'staging'])) { ... }
- Use the config() helper function to access the variable in the application (e.g. in a controller or in a view) e.g.
config('app.name')
: returns the value of the name variable in the config/app.php fileconfig('app.timezone')
returns the value of the timezone variable in the config/app.php fileconfig('mail.from.address')
: returns the value of the from -> address variable in the config/mail.php file