Code Happy: Configuration

← Back to Index

Please note that this chapter was written for VERSION 3 of the Laravel PHP Framework.

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!

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..

<?php

// application/config/ourconfig.php

return array(

    'size'  => 6,

    'eat'   => true,
);

You can use comments to make your config files more descriptive, I like to use the style of the comments Laravel provides, for example..

<?php

// application/config/ourconfig.php

return array(

    /*
    |------------------------------
    | Size
    |------------------------------
    |
    | This is the size of my thing.
    |
    */

    'size'  => 6,
);

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..

<?php

// application/config/ourconfig.php

return array(

    /*
    |------------------------------
    | Size
    |------------------------------
    |
    | This is the size of my thing.
    |
    */

    'size'  => function() {
        $size = file_get_contents('path/to/file.json');
        $size = json_decode($size);
        return $size;
    },
);

Now our 'size' configuration is read from the JSON contents of a file, simple!

Reading Configuration

We can read a configuration setting using the get() method..

<?php

$option = Config::get('ourconfig.size');

Simply pass a string to the method, with the name of the file a period (.) and the name of the configuration key. The value will be returned. If your configuration file is in a subdirectory you will need to use extra periods to indicate the subdirectories, for example..

<?php

$option = Config::get('ourconfig.sub.directory.size');

Sometimes it's useful to retrieve the entire configuration array, to do this simply specify the filename without the option. To retrieve the entire configuration array from our file we would use.

<?php

$option = Config::get('ourconfig');

Setting Configuration

To set a configuration item, use the set() method. The first parameter represents the file and configuration item name, in the same format as the get() method. The second parameter is the value you wish to set.

<?php

Config::set('ourconfig.size', 7);

If a configuration item exists within a configuration file, it will be written in the runtime configuration when you use set(), however it will not be overwritten in the configuration file itself. If a configuration item doesn't exist, a call to the set() method will create it, but again not within the configuration file.

Try to put as many configurable settings as possible from your application into configuration files, it will make it much easier to configure if you have to move or redistribute the application.

My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.

It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new books from the comfort of my sofa!