Configuration
Laravel has many configuration files in application/config to tweak almost every feature that
the framework offers. Wouldn’t it be great if you could create your own configuration files this
way? Well today is your lucky day, because you can!
19.1 Creating new Configuration Files
Laravel config files are simply PHP files that live in application/config or a subdirectory, and
return a PHP array. For example..
1 <?php
2
3 // application/config/ourconfig.php
4
5 return array(
6
7 'size' => 6,
8
9 'eat' => true,
10 );
You can use comments to make your config files more descriptive, I like to use the style of the
comments Laravel provides, for example..
1 <?php
2
3 // application/config/ourconfig.php
4
5 return array(
6
7 /*
8 |------------------------------
9 | Size
10 |------------------------------
11 |
12 | This is the size of my thing.
13 |
14 */
15
16 'size' => 6,
17 );I’m sure you can come up with a better description! You will have noticed by now that Laravel
configuration files are key-value pairs, with the array index representing the key, and the value…
its value.
The value of the setting can be any value or object that PHP supports, it can even be a closure.
By providing a closure you are making it easy for the user to change the configuration to enable
it to be loaded from another source, for example..
1 <?php
2
3 // application/config/ourconfig.php
4
5 return array(
6
7 /*
8 |------------------------------
9 | Size
10 |------------------------------
11 |
12 | This is the size of my thing.
13 |
14 */
15
16 'size' => function() {
17 $size = file_get_contents('path/to/file.json');
18 $size = json_decode($size);
19 return $size;
20 },
21 );
Now our ‘size’ configuration is read from the JSON contents of a file, simple!