Global Constants in CodeIgniter 4 Tutorial

Share this Article

In CodeIgniter 4 application, we have several global constants available. Global constant simply means variables which we can access anywhere inside application at any point. Value for constants is fixed for lifetime. We can’t alter values for those.

Inside this article we will see the detailed concept about Global constants in CodeIgniter 4. This article will be very interesting to learn and know the new things in codeigniter 4.

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 get started.


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.


Settings Environment Variables

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.


What is a Global Constant?

Global constants are also termed as core constants of application. It simply means theses –

  • Variables which can accessed through out application means globally available to use.
  • Values of constants can’t be altered upto it’s lifetime.
  • We can define constant using PHP function define()

Inside CodeIgniter 4, we have several global constants. We have global constants into two sections – Core and Time. Here is the list of those –

Core Global Constants

Core simply means application related constants.

  • APPPATH
  • FCPATH
  • ROOTPATH
  • SYSTEMPATH
  • WRITEPATH

Time Global Constants

Time means time related global constants.

  • DAY
  • DECADE
  • HOUR
  • MINUTE
  • MONTH
  • SECOND
  • WEEK
  • YEAR

These all global constants we can find at /app/Config/Constants.php file.


CodeIgniter 4 Global Constants

Let’s see the detailed concept about global constants of CodeIgniter 4.

APPPATH

This constant returns the path to the app directory. app directory is available at application root.

This directory contains folders for all like Config, Controllers, Models etc.

ROOTPATH

It returns the path to the project root directory. Means the application path.

It contains all application files and folders.

SYSTEMPATH

The path to the system directory. system directory is available at application root. In some setup you will not able to see this directory instead you will get vendor folder. Simply it contains application core files.

If containing vendor then SYSTEMPATH equals to /vendor/codeigniter4/framework/system/

FCPATH

The path to the directory that holds the front controller. Means path of public directory. public directory contains all public accessible files like images, css, js etc.

WRITEPATH

The path to the writable directory. This directory is available at application root.

SECOND

It equals to 1 means whenever we use it, we will get 1 value.

MINUTE

It equals 60. Means 60 seconds.

HOUR

It equals 3600. Means 3600 seconds.

DAY

It equals 86400. This is in seconds.

WEEK

It equals 604800. This is in seconds.

MONTH

It equals 2592000. This is in seconds.

YEAR

It equals 31536000. This is in seconds.

DECADE

It equals 315360000. This is in seconds.


How to Use?

Open any php file from application. Inside that file, we need to use same syntax which we use for normal variable.

<?php echo FCPATH ?>

OR

<?= FCPATH ?>

We hope this article helped you to learn about Global Constants in CodeIgniter 4 Tutorial 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

What are CSRF Functions in CodeIgniter 4 Tutorial
MySQL Stored Procedure in CodeIgniter 4 Tutorial