📖 Scenario: You are building a simple Laravel app that needs to store and access configuration settings for a blog.These settings include the blog's title, the number of posts to show per page, and whether comments are enabled.
🎯 Goal: Create a Laravel config file with specific settings and access these settings in a controller.
📋 What You'll Learn
Create a config file named blog.php in the config directory
Add specific configuration keys and values to blog.php
Access the config values using Laravel's config() helper in a controller
Return the config values as an array from a controller method
💡 Why This Matters
🌍 Real World
Config files help keep app settings organized and easy to change without touching code logic. This is common in real Laravel projects.
💼 Career
Understanding config files and accessing them is essential for Laravel developers to build flexible and maintainable applications.
Progress0 / 4 steps
1
Create the blog config file
Create a config file named blog.php inside the config directory with the following exact array keys and values: 'title' => 'My Laravel Blog', 'posts_per_page' => 5, and 'comments_enabled' => true.
Laravel
Hint
Remember, Laravel config files return an array of key-value pairs.
2
Add a controller method to access config
In a controller named BlogController, create a public method called settings that will access the config values using config('blog.title'), config('blog.posts_per_page'), and config('blog.comments_enabled').
Laravel
Hint
Use Laravel's config() helper to get values from your config file.
3
Return config values as an array
Inside the settings method of BlogController, return an array with keys 'title', 'posts_per_page', and 'comments_enabled' set to the respective config values you accessed.
Laravel
Hint
Return an associative array with the config values as keys and variables as values.
4
Add route to access settings method
In the routes/web.php file, add a route that responds to /blog/settings URL and uses BlogController@settings to return the config settings.
Laravel
Hint
Use Laravel's Route::get() method with an array to specify the controller and method.
Practice
(1/5)
1. What is the primary purpose of config files in a Laravel application?
easy
A. To define routes for the application
B. To write database queries
C. To create user interface components
D. To store application settings as PHP arrays
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 D
Quick 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
A. config('app.timezone')
B. config('app->timezone')
C. Config::get('timezone.app')
D. Config::get('app.timezone')
Solution
Step 1: Recall Laravel config access syntax
Laravel uses the helper function config('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 A
Quick 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 config/mail.php contains:
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
A. env('PAYMENT_GATEWAY', 'stripe')];
B. config('PAYMENT_GATEWAY', 'stripe')];
C. 'PAYMENT_GATEWAY' ?? 'stripe'];
D. getenv('PAYMENT_GATEWAY', 'stripe')];
Solution
Step 1: Use env() to read environment variables in config
Laravel config files use env('VAR_NAME', 'default') to get env vars with defaults.
Step 2: Write correct PHP array return
Return an array with key 'gateway' set to env('PAYMENT_GATEWAY', 'stripe').
Final Answer:
<?php
return ['gateway' => env('PAYMENT_GATEWAY', 'stripe')]; -> Option A
Quick 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 unknownstrongwhile earning +1 Grip toward your certificate.