Table of Contents
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.
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.
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.
Time Global Constants
Time means time related global constants.
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.
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.
It returns the path to the project root directory. Means the application path.
It contains all application files and folders.
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/
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.
The path to the writable directory. This directory is available at application root.
It equals to 1 means whenever we use it, we will get 1 value.
It equals 60. Means 60 seconds.
It equals 3600. Means 3600 seconds.
It equals 86400. This is in seconds.
It equals 604800. This is in seconds.
It equals 2592000. This is in seconds.
It equals 31536000. This is in seconds.
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.
Find More on CodeIgniter 4 here
- CodeIgniter 4 Cookie Helper Tutorial
- CodeIgniter 4 CRUD Application Tutorial
- CodeIgniter 4 CRUD REST APIs Tutorial
- CodeIgniter 4 CSRF Token in AJAX Request
- Database Query in CodeIgniter 4 Tutorial
- CodeIgniter 4 Ajax Form Data Submit
- CodeIgniter 4 Form Validation Tutorial
- CodeIgniter 4 Image Upload with Form Tutorial
- Multi language in CodeIgniter 4 Tutorial
- Stripe Payment Gateway Integration in CodeIgniter 4
- CodeIgniter 4 CSRF Token Tutorial
- CodeIgniter 4 Basics Tutorial
- CodeIgniter 4 Spark CLI Commands Tutorial
- Migration in CodeIgniter 4 Tutorial
- Seeders in CodeIgniter 4 Tutorial