This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Before we finalize our application, we'll look at some additional options for specifying required variables, giving developers a template to start with, and overriding environment variables.
Specifying Required Environment Variables
Using Dotenv, you can require specific ENV vars to be defined ($_ENV, $_SERVER or getenv()) - throws an exception otherwise. Note: It does not check for existence of a variable in a '.env' file. This is particularly useful to let people know any explicit required variables that your app will not work without.
You can use a single string, or an array of strings:
$dotenv->required(['REPOSITORY', 'SITE_KEY', 'SECRET_KEY', 'SMTP_DEBUG', 'SMTP_HOST', 'SMTP_PORT', 'SMTP_SECURE', 'SMTP_AUTH', 'SMTP_USERNAME', 'SMTP_PASSWORD', 'SMTP_FROM', 'MAILTO_EMAIL', 'MAILTO_NAME']);
If any ENV vars are missing, Dotenv will throw a RuntimeException like this:
One or more environment variables failed assertions: SITE_KEY is missing
Read more from the phpdotenv documentation
Example Files
Only difference for yahoo is SMTP_HOST="smtp.mail.yahoo.com"
env.gmail.txt
#REPOSITORY must be a json string with "type" and "source" REPOSITORY={ "type":"txt", "source":"inc/quotes.txt" } #Manage your reCAPTCHA API keys: https://www.google.com/recaptcha/admin SITE_KEY=google_recaptcha_site_key SECRET_KEY=google_recaptcha_secret_key #Google SMTP SMTP_DEBUG=2 SMTP_HOST="smtp.gmail.com" SMTP_PORT=587 SMTP_SECURE="tls" SMTP_AUTH=true SMTP_USERNAME=most_likely_your_email SMTP_PASSWORD=app_password SMTP_FROM=most_likely_your_email MAILTO_EMAIL=most_likely_your_email MAILTO_NAME=any_name
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up