Config files store settings for your Laravel app in one place. This helps you change app behavior without editing code everywhere.
Config files and access in Laravel
Introduction
Syntax
Laravel
<?php return [ 'key' => 'value', 'nested' => [ 'subkey' => 'subvalue', ], ];
Config files are PHP files that return an array of settings.
Each config file is stored in the
config/ folder.Examples
Laravel
<?php return [ 'app_name' => 'My Laravel App', 'debug' => true, ];
Laravel
<?php return [ 'database' => [ 'host' => '127.0.0.1', 'port' => 3306, ], ];
env() to get sensitive info from environment variables.Laravel
<?php return [ 'services' => [ 'mailgun' => [ 'domain' => env('MAILGUN_DOMAIN'), 'secret' => env('MAILGUN_SECRET'), ], ], ];
Sample Program
This example shows a config file example.php with two keys. The route /greet reads these values using Config::get() and returns a greeting message.
Laravel
<?php // config/example.php return [ 'greeting' => 'Hello', 'audience' => 'World', ]; // In a Laravel controller or route closure use Illuminate\Support\Facades\Config; Route::get('/greet', function () { $greeting = Config::get('example.greeting'); $audience = Config::get('example.audience'); return "$greeting, $audience!"; });
Important Notes
Use Config::get('file.key') to read config values anywhere in your app.
Use env() in config files to keep secrets out of code and use environment variables instead.
After changing config files, you may need to clear config cache with php artisan config:clear.
Summary
Config files hold app settings in config/ as PHP arrays.
Access config values with Config::get('file.key').
Use environment variables for sensitive info with env().
Practice
1. What is the primary purpose of config files in a Laravel application?
easy
Solution
Step 1: Understand config file role
Config files in Laravel are PHP files that return arrays holding settings.Step 2: Identify correct purpose
They store app settings like database, mail, and cache configurations.Final Answer:
To store application settings as PHP arrays -> Option DQuick Check:
Config files = app settings [OK]
Hint: Config files hold settings, not code or UI [OK]
Common Mistakes:
- Confusing config files with route or view files
- Thinking config files contain SQL queries
- Assuming config files define UI elements
2. Which of the following is the correct syntax to access the 'timezone' setting from the 'app' config file in Laravel?
easy
Solution
Step 1: Recall Laravel config access syntax
Laravel uses the helper functionconfig('file.key')to get config values.Step 2: Identify correct syntax
config('app.timezone') uses the correct helper function and dot notation for 'app.timezone'.Final Answer:
config('app.timezone') -> Option AQuick Check:
Use config('file.key') for access [OK]
Hint: Use config() helper with dot notation for keys [OK]
Common Mistakes:
- Using arrow (->) instead of dot (.) in key
- Using Config::get() without importing facade
- Swapping file and key order
3. Given the config file
And the environment variable
config/mail.php contains:return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailtrap.io'),
];And the environment variable
MAIL_DRIVER is set to sendmail, what will config('mail.driver') return?medium
Solution
Step 1: Understand env() usage in config
The config usesenv('MAIL_DRIVER', 'smtp'), which returns the environment variable if set, else 'smtp'.Step 2: Check environment variable value
SinceMAIL_DRIVERis set to 'sendmail',env('MAIL_DRIVER', 'smtp')returns 'sendmail'.Final Answer:
"sendmail" -> Option BQuick Check:
env() returns env var if set, else default [OK]
Hint: env() returns env var first, default second [OK]
Common Mistakes:
- Assuming default value always returns
- Confusing config key with env variable name
- Ignoring environment variable settings
4. What is wrong with this code snippet to access a config value?
use Illuminate\Support\Facades\Config;
$value = Config::get('database.connections.mysql.host');medium
Solution
Step 1: Check import statement
The code imports Config facade correctly withuse Illuminate\Support\Facades\Config;.Step 2: Verify config key syntax
The key 'database.connections.mysql.host' uses correct dot notation to access nested config arrays.Final Answer:
Nothing is wrong; this is correct syntax -> Option CQuick Check:
Config::get() with dot keys and correct import is valid [OK]
Hint: Dot notation accesses nested config keys correctly [OK]
Common Mistakes:
- Using underscores instead of dots in keys
- Forgetting to import Config facade
- Assuming key length causes errors
5. You want to create a new config file
config/payment.php with a key gateway that uses an environment variable PAYMENT_GATEWAY defaulting to 'stripe'. How should you write the config file to access this value correctly in Laravel?hard
Solution
Step 1: Use env() to read environment variables in config
Laravel config files useenv('VAR_NAME', 'default')to get env vars with defaults.Step 2: Write correct PHP array return
Return an array with key 'gateway' set toenv('PAYMENT_GATEWAY', 'stripe').Final Answer:
<?php return ['gateway' => env('PAYMENT_GATEWAY', 'stripe')]; -> Option AQuick Check:
Use env() in config files for env vars [OK]
Hint: Use env() with default inside config files [OK]
Common Mistakes:
- Using config() inside config files instead of env()
- Using getenv() without fallback default
- Using PHP null coalescing incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
