Appearance
.env file
PODCAST
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
KEY=VALUE
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 SQLite for the environment, but you can also use MySql.
Install MySQL Workbench and PhpMyAdmin
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 need to adjust the following variables in the .env file:
- change DB_CONNECTION=sqlite to DB_CONNECTION=mysql
- Uncomment the variables DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME and DB_PASSWORD and add the correct values. we are not allowed to use port 3306, but port 2200.
php
DB_CONNECTION=sqlite // change sqlite to mysql
DB_HOST=127.0.0.1 // try localhost if 127.0.0.1 doesn't work
DB_PORT=3306
DB_DATABASE=myProject
DB_USERNAME=root
DB_PASSWORD=
1
2
3
4
5
6
2
3
4
5
6
MAIL_...
- To test the mail functionality we use the locally installed Mailpit 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
- Start the Mailhog server with the command
mailpit
- The Mailhog inbox is available at: http://localhost:8025
php
MAIL_MAILER=smtp # change log to smtp
MAIL_HOST=127.0.0.1
MAIL_PORT=1025 # change 2525 to 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"
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.
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...
}
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
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');
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>
...
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9