How to create Custom Config File in CodeIgniter 4

In this example I am going to show you how to create and work with custom config file in CodeIgniter 4 framework. A PHP based CodeIgniter 4 framework is the new and future version of the CodeIgniter framework and currently version 4 is still under development.

Config file is generally used to declare different configurations for your application. Configurations may be environment variables, database variables, or any other variables or settings you want to do based on a specific environment or profile.

The similar configurations you can do using the env file which is by defaylt shipped with CodeIgniter 4 framework. You just need to rename the env file as .env file and whatever configuration you want you can change or add.

Prerequisites

PHP 7.4.3, CodeIgniter 4.0.4

Project Directory

It’s assumed that you have setup PHP and CodeIgniter in Windows system.

Now I will create a project root directory called codeigniter-4-custom-config.

Now move all the directories and files from CodeIgniter framework into the project root directory.

I may not mention the project root directory in subsequent sections and I will assume that I am talking with respect to the project root directory.

Custom Config

Here I am going to tell you how to create a config file, then how to declare some configuration variables and how to use those configuration variables in controller or view file.

The config file is generally created under app/Config folder. Your config class needs to extend the BaseConfig class.

Let’s say you want to have few default configurations for your site. So I am creating a config class Site that extends BaseConfig in a PHP file Site.php under app/Config folder.

<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

/**
* Author: https://roytuts.com
*/

class Site extends BaseConfig {
	
    public $defaultTitle = 'Roy Tutorials';
    public $defaultSuccessMsg = 'Success';
    public $defaultErrorMsg = 'Error';
    public $displayMsg = 1;
    public $authRequired = false;

}

Access Config

You can access your custom config variables from controllers, models, libraries, etc. You can access in the constructor of your class or you can access inside the function as well.

To access inside your class’s constructor you can use the following example:

function __construct() {
	$config = new \Config\Site();
}

You don’t need to declare any namespace. You just need to make sure that you use your class’s name (Site) along with Config as shown in the above example.

Here I am going to access the custom config from controller class. You may access from model or libraries also. The following code I have written into Home controller.

<?php namespace App\Controllers;

class Home extends BaseController {
	
	public function index()	{
		$config = new \Config\Site();
		
		$data['default_title'] = $config->defaultTitle;
		$data['auth_required'] = $config->authRequired;
		
		return view('welcome_message', $data);
	}

}

In the above index() function I have accessed only two variables though I declared few more variables in the custom config class. You can access all of them.

View File

View file where I am actually going to display values for the config variables.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Welcome to CodeIgniter 4!</title>
	<meta name="description" content="The small framework with powerful features">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="shortcut icon" type="image/png" href="/favicon.ico"/>
</head>
<body>

<p>
	<?php echo $default_title; ?>
</p>
<p>
	<?php echo $auth_required === true ? 'Authentication Required' : 'Authentication Not Required'; ?>
</p>

<footer>
	<div class="environment">

		<p>Page rendered in {elapsed_time} seconds</p>

		<p>Environment: <?= ENVIRONMENT ?></p>

	</div>

</footer>

</body>
</html>

Testing the Application

You can access the application via CLI interface by executing the command php spark serve on the project’s root directory from the command line tool.

Now access the application in the browser via URL http://localhost:8080.

The config variable’s value is highlighted in the below figure:

work with custom config file in codeigniter 4

Source Code

Download

Leave a Comment