How To Define Global Constant in CodeIgniter 4 Tutorial
Inside this article we will see the concept to define global constant in codeigniter 4. 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.
List of all available global constants in CodeIgniter 4, Click here for article.
Here we will discuss to create global constant step by step and how to use in codeigniter 4 application.
Table of Contents
- Download & Install CodeIgniter 4 Setup
- Settings Environment Variables
- Define Global Constant in Application
Let’s get started.
Here is the command to install via composer –
$ composer create-project codeigniter4/appstarter codeigniter-4
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
Define Global Constant in Application
Global constants can be define in two ways inside Codeigniter 4 application. These are the following ways –
- By using .env file
- By using Constants.php file