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.
Learn More –
- Javascript Auto Logout in CodeIgniter 4 Tutorial
- jQuery UI Autocomplete Database Search CodeIgniter 4
- Learn CodeIgniter 4 Tutorial From Beginners To Advance
- Line Chart Integration with CodeIgniter 4 – HighCharts Js
Let’s get started.
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.
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.
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.