Create Custom Config File in CodeIgniter 4

Share this Article
Reading Time: 3 minutes
582 Views

CodeIgniter 4 by default have many configuration files. Each configuration file is for different different task. Like initializing logger settings, migration settings, defining constants, database setup etc.

Now, inside this article we will see the concept to create custom config file in CodeIgniter 4. We will save custom configurations what we need to process the application. This article is going to be more exciting to learn.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s start this article step by step.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


Create Custom Config

We will create config file via spark command line. To create config we use make:config

Open project into terminal and type this command.

$ php spark make:config MyConfig

This command will creates a file with name MyConfig.php inside /app/Config folder. Let’s open this file and add some piece of code into it.

<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

class MyConfig extends BaseConfig
{
	public $name = "Online Web Tutor";
	public $author = "Sanjay Kumar";

	public function get_info(){
		return $this->name. " - ". $this->author;
	}
}

As we can see, we have added two public member variables ($name, $author) and one member function (get_info()).


Load Custom Config Inside Application

Next, if say we want to load this custom config into any of controller. Let’s make a controller using spark command.

$ php spark make:controller Site

It will create a file with the name Site.php into /app/Controllers folder.

Open Site.php

<?php

namespace App\Controllers;
use Config\MyConfig; // Loading config class

class Home extends BaseController
{
	public function index()
	{
		$myconfig = new MyConfig; // Creating an instance
      
        echo $myconfig->author; // Accessing member variable

		echo $myconfig->get_info(); // Accessing member function
	}
}

Output

$myconfig->author outputs Sanjay Kumar

$myconfig->get_info() outputs Online Web Tutor - Sanjay Kumar

We hope this article helped you to learn Create Custom Config File in CodeIgniter 4 in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here

Leave a Comment