Folder structure of a project in CodeIgniter 4

Video thumbnail

CodeIgniter 4 is an open-source web development framework designed to facilitate the rapid and agile development of PHP web applications. It brings a large number of features compared to the size of the framework; it is ideal for taking the first steps in web application development with frameworks. We have already seen how to perform various operations, such as the Command line in CodeIgniter 4, Spark.

A project in CodeIgniter 4 has the following organization, for which we will explain the general purpose of each folder and file:

app Folder

Inside the app folder is where all the application code resides and where we will spend most of our time; this folder comes with a default structure of folders and files that works well for many applications. The following folders make up the content:

  1. /Config Stores configuration files; they are varied, for example, database settings, form validations, emails, filters, among others.
  2. /Controllers Controllers determine the program flow; they are one of the three elements of our MVC and communicate the view with the model according to the rules we define.
  3. /Database Stores database migrations and seed files (seeds); we talk about this in another chapter.
  4. /Filters Stores filter-type classes that can be executed before and after the specific controllers you specify.
  5. /Helpers Helper files that store collections of independent functions that we can easily reuse throughout the application.
  6. /Language Here we define files to manage the application's languages.
  7. /Libraries Useful classes that do not fit into another category; they are more specific schemas than helpers, where we can rely on classes to create functions that we can reuse.
  8. /Models Models are classes that have a direct relationship between tables and classes, and columns and class properties; here we define formats, structures, and essentially communication with the database; this is one of the three elements corresponding to our MVC.
  9. /ThirdParty Third-party libraries that can be used in applications.
  10. /Views Views compose the HTML displayed to the client and are another layer of our MVC; it is assigned from the controller.

system folder

This folder stores the files that make up the framework itself; all the features of our framework reside here, therefore, files in the system folder should never be modified. Instead, you can add your features through helpers (files with helper functions) or create new classes to provide the desired functionality.

All files in this folder are under the "CodeIgniter" namespace.

public folder

The public folder contains the browser-accessible part of your web application; it is the only folder that can be accessed, and this is for security reasons; this is an important change from version 3 and follows the same steps employed by other frameworks like Laravel, which also have this same structure.

Placing a single publicly accessible folder prevents direct access to your source code. It contains the main .htaccess file, index.php, and any application resources you add, such as CSS, JavaScript, and images.

This folder is intended to be the "web root" of your site, and your web server would be configured to point to it.

Other important folders

writable folder

This folder contains all the folders and files that may need to be written to during the lifespan of an application.

This includes files and folders generated by the framework itself, as well as folders for storing cache files, logs, and any uploads a user might send. This allows you to keep your other primary folders non-writable as an additional security measure, preventing the upload and execution of unwanted code.

If you upload images here, they will not be directly visible from the browser, as only the public/ folder is accessible. We will see how to fix this later.

tests folder

This folder is set up to contain your test files.

It is not necessary to transfer this folder to your production servers.

docs folder

This folder may or may not be present; it depends on how you obtain your copy of the framework. It contains a local copy of the CodeIgniter 4 user guide so you know what to do; it is part of the CodeIgniter website (documentation section) but in PDF version.

Root files

Finally, we have configuration files such as:

  • Composer.json: Manages dependencies and the required PHP version (8.2 or higher).
  • .env: File for environment variables (currently disabled by default), which we will see later.

With this, you now have a general overview of how a project is organized in CodeIgniter 4.

Now, learn how to run your CodeIgniter 4 application.

Learn the CodeIgniter 4 file structure step by step. Discover the purpose of the App, Public, and Writable folders, and master the framework's MVC architecture.

I agree to receive announcements of interest about this Blog.

Andrés Cruz

ES En español