Content Index
CodeIgniter 4 is an open-source web development framework designed to facilitate rapid and agile development of PHP web applications. Despite its small footprint, it offers a wealth of features and is ideal for taking your first steps with PHP frameworks. If you arrived here from the previous chapter, we already covered how to work with the command line in CodeIgniter 4 with Spark.
In this chapter, we will see how a CodeIgniter 4 project is organized, explaining the purpose of each folder and most relevant file. Knowing this structure well from the beginning is essential: it will save you time searching for where to put everything and give you a clear vision of the framework's flow from day one.
app Folder
Inside the app folder resides all your application's code, and it is where you will spend most of your development time. It comes with a default subfolder structure that works well for most projects. These are the folders that compose it:
app/Config— Stores all project configuration files: database, form validations, emails, HTTP filters, routes, and much more. Every aspect of the framework has its own configuration file in this folder.app/Controllers— Controllers determine the program flow and are one of the three pillars of the MVC pattern. Their role is to receive the HTTP request, interact with the model if necessary, and return the corresponding view to the client.app/Database— Database migrations and seed files are stored here. Migrations allow you to version control your database structure along with code, while seeds make it easy to populate it with test data. We will discuss both in later chapters.app/Filters— Stores filter classes that can be executed before (before) or after (after) controllers. They are very useful for cross-cutting tasks such as authentication, access control, or request throttling.app/Helpers— Files containing collections of independent, reusable global functions. Unlike classes, helpers are simple PHP functions you can call anywhere in the application without instantiating anything.app/Language— Translation files for handling multiple languages in the application are defined here. Each language has its own subfolder with the corresponding texts.app/Libraries— Specific utility classes that do not fit into another category. Unlike helpers (loose functions), libraries are object-oriented classes you can instantiate and reuse when you need more structured logic.app/Models— Models are classes that represent and manage communication with the database. Each model typically corresponds to a table, and through it you define data structure, validations, and queries. It is one of the three elements of the MVC pattern.app/ThirdParty— Folder reserved for third-party libraries integrated manually into the application (bypassing Composer).app/Views— Views contain the HTML displayed to the client and represent the third layer of MVC. They are assigned and rendered from the controller via thereturn view('view_name')method.
system Folder
This folder contains the framework core: all base classes, internal libraries, and functionality upon which CodeIgniter 4 is built. It is the internal "machinery" of the framework, so files inside system should never be modified directly. If you do, you will lose those changes as soon as you update the framework.
Instead of modifying this folder, extend functionality by creating your own helpers in app/Helpers, new classes in app/Libraries, or overriding core classes from the app folder. All files in system belong to the CodeIgniter namespace.
public Folder
The public folder is the only one that should be accessible from the browser. It is the web server's document root, meaning the folder Apache or Nginx should point to. This design is intentional and represents a significant security improvement over CodeIgniter 3: by exposing only this folder, the rest of the source code —including configuration and credentials— remains completely out of the browser's reach.
This same strategy is adopted by other modern frameworks like Laravel, which also uses a public folder as its web root. Inside it you will find:
.htaccess— URL rewriting rules for Apache (removesindex.phpfrom the URL).index.php— Entry point for the entire application.- Static assets: CSS, JavaScript, images, and any file needing direct browser access.
Server Configuration: When deploying your application, make sure your server's document root points to the
public/folder, not the project root. Otherwise, you would expose sensitive files like.envor theapp/Configfolder.
Other Important Folders
writable Folder
This folder is designed to contain all files and directories where the application needs to write during its lifecycle. It includes:
writable/cache— Cache files generated by the framework.writable/logs— Application error and event logs.writable/uploads— Files uploaded by users (or another folder you define for it).writable/session— Session data when using the file driver.
Keeping the rest of the primary folders (app, system) without write permissions is an additional security measure preventing an attacker from uploading and executing malicious code in those paths.
If you upload images to
writable/uploads, they will not be directly visible from the browser, since only thepublic/folder is accessible. Later we will see how to properly manage uploaded files and serve them to the client.
tests Folder
This folder is configured to house all your automated test files. CodeIgniter 4 includes native support for PHPUnit, allowing you to write unit and integration tests from the very beginning.
It is not necessary to deploy this folder to your production servers; it is only relevant in the development environment and continuous integration pipelines (CI/CD).
docs Folder
This folder may or may not be present depending on how you obtained the framework. When it exists, it contains a local copy of the official CodeIgniter 4 user guide in PDF format; basically, it is the official framework website documentation packaged for offline consultation.
Root Project Files
Finally, at the project root you will find global configuration files affecting the development environment and dependency management:
composer.json— Manages project PHP dependencies and defines the minimum required PHP version (8.1 or higher for CI4). PSR-4 autoloading namespaces are also configured here..env— Environment variables file allowing configuration of aspects like application mode (CI_ENVIRONMENT), database credentials, and other sensitive keys without hardcoding them into code. By default it comes renamed asenv(without a dot), so you must copy and rename it to.envbefore use.spark— The CodeIgniter 4 command-line interface executable. It is used withphp spark [command]for tasks like starting the development server, creating migrations, generating controllers, and much more.
With this you now have a clear and complete overview of how a CodeIgniter 4 project is organized. Understanding this structure from the beginning will allow you to move fluidly through the framework and know exactly where each piece of your application lives.
Now, learn how to run your CodeIgniter 4 application in the browser.