Another in the CakePHP3 quick tip series - this time I'm going to explain how you can go about creating your own configuration files - useful when building an app such as a CMS.

Before I jump in to the technical side of it, let's discuss a bit of theory - take this website - it's built on a custom CMS pieced together using CakePHP 3, the CMS has multiple features. Including, but not limited to: Blog posts, pages and multimedia. Within the CMS there are a number of features that may have absolutely no relevancy to the project I'm embarking on - some projects have absolutely no use for a blog, which is precisely why I have a set of custom configuration objects that can enable and disable features without having to adjust the code anywhere.

CakePHP 3 makes custom configuration a very simple task - first of all, we need to create our config file - I've called mine cms.php which I've created within the /config directory. Once you've created the file we need to then add it to the /config/bootstrap.php file to tell the app to load it in.

Configure::load('cms');

In your bootstrap.php file, there is a section which starts with a comment "Load an environment local configuration file" - add the line above under the comments.

We now need to create our config array, within mine I've created an array called "gtcms_modules", any of the modules listed in the array set to true are considered to be enabled.

The key to your array will be how we retrieve the configuration options, take a look at the code below.

<?php
    $config = [
        'gtcms_modules' => [ 
        'blog' => true 
    ] 
];

Simple, right? We first assign an array to the $config variable and within that we can create our options and give them a key that we can use throughout the app.

So, how do we retrieve these options? Let's take a look at a basic example within a controller.

<?php 
namespace App\Controller; 
use App\Controller\AppController; 
use Cake\Event\Event; 
use Cake\Core\Configure; 
use Cake\Network\Exception\NotFoundException; 

class PostsController extends Controller { 
    public function beforeFilter(Event $event) { 
        // Grab the gtcms_modules config option 
        $gtcms_modules = Configure::read('gtcms_modules'); 

        // Check if the blog module is enabled 
        if(!$gtcms_modules['blog']){ 
            throw new NotFoundException('It appears you followed an incorrect URL...'); 
        } 
    } 
}

So to break down the above, first we need to reference our Configure class - you will notice I've also included the NotFoundException class for throwing an error to the user.

We then need to grab our modules from within the config file created earlier and use that to check whether the blog module is set to true, to do that we call the read method from the Configure class and give it the key of the array we created earlier to retrieve the specified options.

If the module doesn't return a true value we throw a 404 error with the message "It appears you followed an incorrect URL...".

Pretty simple stuff.

As usual, any comments - drop them below - I try to respond to them all.

Thanks for reading!