agyanadda@gmail.com

CodeIgniter 4 Configuration and .env

CodeIgniter 4 Basics 1 min read Updated Dec 3, 2026 Topic 4 of 30

CodeIgniter 4 keeps settings in config classes in app/Config, and lets the .env file override them per server.

The .env file

  • CI_ENVIRONMENT — development (shows errors) or production (hides them). Always production on a live site.
  • app.baseURL — the site address, ending with /
  • database.default.hostname, .database, .username, .password, .DBDriver
  • encryption.key — create with php spark key:generate

The name format is class.property: app.baseURL overrides $baseURL in app/Config/App.php.

Config classes

Each file in app/Config is a class, for example App.php, Database.php, Email.php, Filters.php, Routes.php.

Reading config in code

  • config('App')->baseURL
  • config(\Config\Email::class)->fromEmail
  • env('MY_KEY', 'default') — read a raw .env value

Your own config

Create app/Config/Site.php that extends BaseConfig with public properties, then read it with config('Site').

Never commit .env to Git — it contains passwords.

Example

Example — A custom config class PHP
<?php
// app/Config/Site.php
namespace Config;

use CodeIgniter\Config\BaseConfig;

class Site extends BaseConfig
{
    public string $siteName = 'AgyanAdda';
    public string $whatsapp = '';
    public int $perPage = 12;
}

// .env (overrides the class values)
// site.whatsapp = 919876543210

// In a controller or view:
$site = config('Site');
echo $site->siteName;   // AgyanAdda
echo $site->whatsapp;   // 919876543210 (from .env)

The .env line site.whatsapp overrides the $whatsapp property of the Site class, so each server can have its own value.

Test Yourself With Exercises

Exercise 1 Multiple Choice

What should CI_ENVIRONMENT be on a live website?

Exercise 2 True / False

The .env key app.baseURL overrides $baseURL in app/Config/App.php.

Exercise 3 Fill in the Blank

Read a config class with the ___('App') helper.

Frequently Asked Questions

With CI_ENVIRONMENT = production, CodeIgniter hides error details. Set it to development on your own computer to see the error, or check the log files in writable/logs.