Appearance
.env file
A .env file contains environment variables. Ii conatains usually stuff that you wouldn't want the user to see. In addition to the Laravel variables, you can expand this file with your own variables. Think of API keys for external services such as Google Maps, DarkSky, etc.
These syntax rules apply to the .env file:
- Compose expects each line in an env file to be in VAR=VAL format.
- Lines beginning with # are processed as comments and ignored.
- Blank lines are ignored.
APP_DEBUG
Use APP_DEBUG=true ONLY in a test environment. In a production environment you always use false. See the output if there is an error in the code. On the left with APP_DEBUG=true and on the right with APP_DEBUG=false.
APP_URL
Specify the URL for the project. For the test environment APP_URL=http://vinyl_shop.test/ is sufficient.
In production you will of course use your real URL like: APP_URL=https://thomasmore.be.
DB_...
We use a MySql database for all our projects. Each project uses its own database.
If you have a project myProject, then it is best to use PhpMyAdmin to create a database with the same name myProject.
In the basic configuration you only need to adjust three variables.
Note that the MySql uses port 3306 by default. Because our database is located within the virtual environment of Homestead, we are not allowed to use port 3306, but port 2200.
php
# DEFAULT # settings for myProject
DB_CONNECTION=mysql
DB_HOST=localhost # change to localhost, try 127.0.0.1 or 192.168.56.56 if localhost doesn't work
DB_PORT=2200 # change to 2200
DB_DATABASE=myProject # change to myProject
DB_USERNAME=homestead # change to homestead
DB_PASSWORD=secret # change to secret
# DEFAULT # settings for myProject
DB_CONNECTION=mysql
DB_HOST=localhost # change to localhost, try 127.0.0.1 or 192.168.56.56 if localhost doesn't work
DB_PORT=2200 # change to 2200
DB_DATABASE=myProject # change to myProject
DB_USERNAME=homestead # change to homestead
DB_PASSWORD=secret # change to secret
1
2
3
4
5
6
7
2
3
4
5
6
7
Open the file config/database.php and see how these variables are used within the application.
MAIL_...
- To test the mail functionality we use the build-in Mailhog server.
- This service simulates the work of a real SMTP server
- It isolates the staging emailing from production and eliminates any possibility of a test email to land in a real customer’s mailbox
- The Mailhog inbox is available at: http://localhost:8025 or http://vinyl_shop.test:8025
php
MAIL_MAILER=smtp
MAIL_HOST=localhost # replace 'mailhog' with 'localhost'
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="info@vinyl_shop.test" # replace "hello@example.com" with your mailadres e.g. "info@vinyl_shop.test"# change to your email address
MAIL_FROM_NAME="${APP_NAME}" # or your name here e.g. "John Doe"
MAIL_MAILER=smtp
MAIL_HOST=localhost # replace 'mailhog' with 'localhost'
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="info@vinyl_shop.test" # replace "hello@example.com" with your mailadres e.g. "info@vinyl_shop.test"# change to your email address
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 contain 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 and will disappear after a few hours.
- Open the file config/mail.php and see how these variables are used within the application.
Determine the environment
- The current application environment (local, staging or production) is determined via the APP_ENV variable from 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...
}
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
@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
You may add additional variables to .env. Think of API keys from external services such as Google maps or OpenWeatherMap.
All these variables can now be used in the application at any time. For example in the router, in a controller and even directly in Blade. Here is an example:
php
// web.php
Route::get('mail', function () {
$me = ['name' => config('mail.from.name')];
return view('mail', $me);
})->name('mail');
// web.php
Route::get('mail', function () {
$me = ['name' => config('mail.from.name')];
return view('mail', $me);
})->name('mail');
1
2
3
4
5
2
3
4
5
php
<!-- resources/views/mail.blade.php -->
...
<h2>I'm {{ $name }}</h2>
<p>You can contact me at
<a class="text-sky-600 underline" href="mailto:{{ config('mail.from.address') }}">
{{ config('mail.from.address') }}
</a>
</p>
...
<!-- resources/views/mail.blade.php -->
...
<h2>I'm {{ $name }}</h2>
<p>You can contact me at
<a class="text-sky-600 underline" href="mailto:{{ config('mail.from.address') }}">
{{ config('mail.from.address') }}
</a>
</p>
...
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9