Understanding how to retrieve the path to the public directory in CodeIgniter 4 is a basic component of handling files and assets within your online application.
When you install CodeIgniter 4, initially application runs from /public folder. By default, It is the path of front controller. There are several pre defined CodeIgniter 4 global constant variables. Those variables returns the path of folders like /app, /writable, etc.
This tutorial will walk you through the process of obtaining the path to the public directory in CodeIgniter 4. We’ll look at how to use the built-in utility function public_path() to find the location of the public directory.
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 Variable in Codeigniter 4?
A global constant variable in CodeIgniter 4 is a value that remains persistent and accessible throughout the application’s runtime.
Global constants are often defined at the application’s top level and retain their value regardless of where they are accessed. They are handy for storing values that must be consistent across multiple portions of your programme.
Global constants in CodeIgniter 4 are defined using the
define() function, which is a built-in PHP function for creating constants.
Examples: APPPATH, ROOTPATH, etc.
Codeigniter 4 Default Global Constants
There are several global constants available in application to use.
How to use & access the path value?
APPPATH (Application folder path)
echo APPPATH; // returns the complete path upto /app folder.
FCPATH (Front controller path)
echo FCPATH; // returns the complete path upto /public folder.
ROOTPATH (Application root)
echo ROOTPATH; // returns the complete path upto / folder.
WRITEPATH (Writable folder path)
echo WRITEPATH; // returns the complete path upto /writable folder.
SYSTEMPATH (System folder path)
echo SYSTEMPATH; // returns the complete path upto /system folder.
How to Create User Defined Constant?
Optionally, you can also create a global constant for the path upto /public folder.
Open Constants.php from /app/Config folder.
Add this line of code into it.
defined('PUBLIC_PATH') || define('PUBLIC_PATH', ROOTPATH . 'public');
ROOTPATH returns the path of application root. Only you need to concat with public folder.
Inside any file you can call as,
echo PUBLIC_PATH; // returns path of /public folder.
We hope this article helped you to learn about How To Get CodeIgniter 4 Public Directory Path 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.