.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_ENV
The current application environment is determined via the APP_ENV variable from your .env file. You may access this value via the environment method on the App facade:
if (App::environment('local')) {
// The environment is local
}
if (App::environment(['local', 'staging'])) {
// The environment is either local OR staging...
}
2
3
4
5
6
7
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://localhost 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 33060..
# DEFAULT # settings for myProject
DB_CONNECTION=mysql
DB_HOST=127.0.0.1 # DB_HOST=localhost
DB_PORT=3306 # DB_PORT=33060
DB_DATABASE=homestead # DB_DATABASE=myProject
DB_USERNAME=homestead
DB_PASSWORD=secret
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 mailtrap.io.
- 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
- Create a free account on mailtrap.io or login with a social provider
- Go to your demo inbox
- You find your personal config variables under SMTP Settings
- Open the .env file and replace all variables that starts with MAIL_
# Copy config from Mailtrap.io
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525 # or 465
MAIL_USERNAME=c6122xxxxxxxxx
MAIL_PASSWORD=f8ca2xxxxxxxxx
MAIL_FROM_ADDRESS=from@example.com # your email here e.g. "john.doe@mailinator.com"
MAIL_FROM_NAME=Example # your name here e.g. "John Doe"
# MAIL_ENCRYPTION=null # delete (or comment) this line
2
3
4
5
6
7
8
9
WARNINGS
- Make sure you are connected to the TM_Intern WiFi network of Thomas More when testing the mail functionality on school premises (if you are connected to the wireless network eduroam, all SMTP/mail ports are blocked!)
- 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.
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.
Access variable with env('key')
You may add additional variables to .env. Think of API keys from external services such as
Google maps or DarkSky.
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:
// web.php
Route::get('contact', function () {
$me = ['name' => env('MAIL_FROM_NAME')];
return view('contact', $me);
});
2
3
4
5
<!-- resources/views/contact.blade.php -->
<h1>I'm {{ $name }}</h1>
<p>You can contact me at
<a href="mailto:{{ env('MAIL_FROM_ADDRESS') }}">
{{ env('MAIL_FROM_ADDRESS') }}
</a>
</p>
2
3
4
5
6
7