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.
Learn More –
- Form Helper Tutorial in CodeIgniter 4
- Form Inputs Validation by Model CodeIgniter 4 Tutorial
- Global Constants in CodeIgniter 4 Tutorial
- How Throttler and Rate limiting work in Codeigniter 4
Let’s start this article step by step.
CodeIgniter 4 Installation
To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.
composer create-project codeigniter4/appstarter codeigniter-4
Assuming you have successfully installed application into your local system.
Environment (.env) Setup
When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env
Either we can do via renaming file as simple as that. Also we can do by terminal command.
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.
Enable Development Mode
CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.
Open .env file from root.
# 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.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
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.