Configuration System in Laravel

Configuration in Laravel

The core settings of your Laravel application — ​database connection settings, queue and mail settings, etc. — live in files in the config folder. Each of these files returns a PHP array, and each value in the array is accessible by a config key that is comprised of the filename and all descendant keys, separated by dots (.).

So, if you create a file at config/credentials.php that looks like this:

// config/credentials.php
<?php
return [
'sparkpost' => [
'secret' => 'abcdefg',
],
];

You can access that config variable using config(‘credentials.sparkpost.secret’).

Any configuration variables that should be distinct for each environment (and therefore not committed to source control) will instead live in your .env files. Let’s say you want to use a different Bugsnag API key for each environment. You’d set the config file to pull it from .env:

// config/credentials.php
<?php
return [
'bugsnag' => [
'api_key' => env('BUGSNAG_API_KEY'),
],
];

This env() helper function pulls a value from your .env file with that same key. So now, add that key to your .env (settings for this environment) and .env.example (template for all environments) files:

# In .env
BUGSNAG_API_KEY=oinfp9813410942
# In .env.example
BUGSNAG_API_KEY=

Your .env file will already contain quite a few environment-specific variables needed by the framework, like which mail driver you’ll be using and what your basic database settings are.

Using env() Outside of Config Files

Certain features in Laravel, including some caching and optimization features, aren’t available if you use env() calls anywhere outside of config files.

The best way to pull in environment variables is to set up config items for anything you want to be environment-specific. Have those config items read the environment variables, and then reference the config variables anywhere within your app:

// config/services.php
return [
'bugsnag' => [
'key' => env('BUGSNAG_API_KEY'),
],
];

// In controller, or whatever
$bugsnag = new Bugsnag(config('services.bugsnag.key'));

The .env File

Let’s take a quick look at the default contents of the .env file. The exact keys will vary depending on which version of Laravel you’re using, but take a look at Example 2–1 to see what they look like.

Example 2–1. The default environment variables in Laravel

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_TIMEZONE=UTC
APP_URL=http://localhost

APP_LOCALE=en
APP_FALLBACK_LOCALE=en
APP_FAKER_LOCALE=en_US

APP_MAINTENANCE_DRIVER=file
APP_MAINTENANCE_STORE=database

BCRYPT_ROUNDS=12
LOG_CHANNEL=stack
LOG_STACK=single
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=sqlite
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=laravel
# DB_USERNAME=root
# DB_PASSWORD=
SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null
BROADCAST_CONNECTION=log
FILESYSTEM_DISK=local
QUEUE_CONNECTION=database
CACHE_STORE=database
CACHE_PREFIX=
MEMCACHED_HOST=127.0.0.1
REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=log
MAIL_HOST=127.0.0.1
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
VITE_APP_NAME="${APP_NAME}"

I won’t go into all of them, because quite a few are just groups of authentication information for various services (Session, Redis, DB, Mail). Here are two important environment variables you should know about, though:

APP_KEY

A randomly generated string that’s used to encrypt data. If this is ever empty, you may run into the error “No application encryption key has been specified.” In that case, just run php artisan key:generate, and Laravel will generate one for you.

APP_DEBUG

A Boolean determining whether the users of this instance of your application should see debug errors — great for local and staging environments, terrible for production.

The rest of the non authentication settings (BROADCAST_DRIVER, QUEUE_CONNECTION, etc.) are given default values that work with as little reliance on external services as possible, which is perfect for when you’re getting started.

When you start your first Laravel app, you shouldn’t have to update any configuration items here in order for the app to serve. Even your database will work out of the box, because Laravel’s installer creates a SQLite database for you and runs your migrations against it.


Configuration System in Laravel was originally published in Javarevisited on Medium, where people are continuing the conversation by highlighting and responding to this story.

This post first appeared on Read More