How To Get CodeIgniter 4 Public Directory Path

Share this Article
Reading Time: 3 minutes
631 Views

Inside this article we will see the concept to get CodeIgniter 4 public directory path. There are several pre defined CodeIgniter 4 global constant variables. These variables returns the path of folders like /app, /writable, etc.

This article contains a classified information about the concept. So you will get the clear vision to get the /public directory path of CodeIgniter 4 application. Public directory basically store the public accessible files as css, js, fonts, images, etc. When you install CodeIgniter 4, initially it runs from /public folder.

Learn More –

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.


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


FCPATH – Global Constant in CodeIgniter 4

There are several global constants in CodeIgniter 4.

For /public directory there is a global constant available i.e FCPATH.

echo FCPATH; // it returns the complete path upto /public folder.

Additionally, we can also create a global constant for this /public folder. Open Constants.php from /app/Config folder.

Add this line into it.

defined('PUBLIC_PATH') || define('PUBLIC_PATH', ROOTPATH . 'public');

ROOTPATH returns the path of application root. we need to concat with public folder.

Inside any controller,

echo PUBLIC_PATH; // returns path of /public folder.

We hope this article helped you to learn How To Get CodeIgniter 4 Public Directory Path Tutorial in a very detailed way.

Buy Me a Coffee

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.

Learn Web Development Courses @ $4 only.
Web Development Courses @ $4