How To Get CodeIgniter 4 Public Directory Path

Share this Article
Reading Time: 3 minutes
12,605 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 application runs from /public folder. It is the path of front controller.

Learn More –

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.


FCPATH – Global Constant in CodeIgniter 4

There are several global constants in CodeIgniter 4.

Default Constant

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

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

User Defined Constant

Additionally, we can also create a global constant for the path upto /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

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.