Lesson 20 of 50 - Learn how to configure Laravel applications using environment variables and configuration files.
Configuration means setting the options that control how an application behaves. Laravel provides configuration files and environment variables to manage these settings.
Examples of configuration include:
Laravel keeps most application configuration files inside
the config directory.
config/
app.php
auth.php
cache.php
database.php
filesystems.php
mail.php
session.php
The exact files and available settings can vary by Laravel version and application setup.
The .env file stores environment-specific
settings, such as the application environment, database
credentials, and debug mode.
Example of a typical Laravel environment file:
APP_NAME="Soopro Pathshala"
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=
SESSION_DRIVER=file
CACHE_STORE=file
These are example values. Use settings appropriate for your own development or production environment.
Important:
.env private and ensure it is excluded from version control.APP_DEBUG=false in production.
The APP_ENV variable identifies the
environment in which your application runs.
APP_ENV=local
Common environment names include:
local — local development.testing — automated testing.production — live application.
The APP_DEBUG variable controls whether
detailed debugging information is displayed when
errors occur.
APP_DEBUG=true
Use true for local development when
appropriate. In production, use:
APP_ENV=production
APP_DEBUG=false
Detailed error pages can expose sensitive application information, so debugging should be disabled on live websites.
Laravel provides the env() helper for
accessing environment values, primarily from
configuration files.
Example:
// In a configuration file
$appName = env('APP_NAME', 'Laravel');
The second argument is the default value used when the environment variable is not available.
For application code, prefer accessing settings through
Laravel's config() helper rather than
calling env() directly.
$appName = config('app.name');
This is especially important when configuration is cached for production.
Use the config() helper to read values
from Laravel configuration files.
Route::get('/app-info', function () {
$appName = config('app.name');
$environment = config('app.env');
$debugMode = config('app.debug');
return [
'application' => $appName,
'environment' => $environment,
'debug' => $debugMode,
];
});
The route returns an array, which Laravel converts into a JSON response.
Some common configuration keys are:
config('app.name');
config('app.env');
config('app.debug');
config('database.default');
config('session.driver');
You can create a custom configuration file inside
the config directory.
Create a file named config/website.php:
<?php
return [
'name' => 'Soopro Pathshala',
'email' => 'info@example.com',
'phone' => '9876543210',
'address' => 'Aurangabad, Bihar',
];
You can read these values using the
config() helper.
$name = config('website.name');
$email = config('website.email');
$phone = config('website.phone');
$address = config('website.address');
For example, config('website.name')
returns Soopro Pathshala.
Laravel's config() helper can also set a
configuration value at runtime.
config([
'website.name' => 'Soopro Library'
]);
$name = config('website.name');
return $name;
The new value is available during the current application
execution. It does not automatically update the
configuration file or permanently change the
.env file.
Avoid using runtime configuration changes as a substitute for proper application settings or persistent storage.
Laravel provides Artisan commands to manage cached configuration.
Clear the configuration cache:
php artisan config:clear
Use this during development when you need Laravel to stop using the cached configuration.
Cache configuration:
php artisan config:cache
This combines configuration into a cached file and is commonly used during production deployment.
Clear application caches:
php artisan optimize:clear
This clears several framework caches. Use it carefully in production because it can affect performance until caches are rebuilt.
After changing environment settings, clear or rebuild configuration cache as appropriate for your deployment. Never run these commands as a substitute for correctly setting production environment variables.
Complete the following tasks:
.env file in your Laravel project.APP_NAME, APP_ENV, and APP_DEBUG.config('app.name').config/website.php with a website name and contact email.php artisan config:clear and php artisan config:cache.Do not publish your actual database passwords, application keys, or other secrets when completing this assignment.
1. Where are Laravel configuration files normally stored?
2. Which helper reads Laravel configuration values?
3. Which command caches Laravel configuration?