Laravel Configuration and Environment

Lesson 20 of 50 - Learn how to configure Laravel applications using environment variables and configuration files.

40%
1. What Is Configuration in Laravel?

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:

  • Application name
  • Database connection details
  • Mail server settings
  • Cache and session drivers
  • Application URL and environment
  • Debugging options

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.

2. What Is the .env File?

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:

  • Do not commit real secrets or passwords to public repositories.
  • Keep .env private and ensure it is excluded from version control.
  • Use APP_DEBUG=false in production.
  • Generate a valid application key for a new application.
3. Understanding APP_ENV and APP_DEBUG

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.

4. Access Environment Variables

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.

5. Read Configuration Values

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');
6. Create Your Own Configuration File

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.

7. Change Configuration Values

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.

8. Important Artisan Configuration Commands

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.

9. Practical Assignment

Complete the following tasks:

  1. Find the .env file in your Laravel project.
  2. Understand the purpose of APP_NAME, APP_ENV, and APP_DEBUG.
  3. Configure a database name for your local development environment.
  4. Read the application name using config('app.name').
  5. Create config/website.php with a website name and contact email.
  6. Display your custom configuration values in a route.
  7. Practice using 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.

Quick Quiz

1. Where are Laravel configuration files normally stored?




2. Which helper reads Laravel configuration values?




3. Which command caches Laravel configuration?