Organization of a project in CodeIgniter 4
CodeIgniter 4 is an open source web development framework, designed to facilitate fast and agile development of web applications in PHP. It brings a large number of features compared to the size of the framework, it is ideal for taking the first steps with the development of web applications with frameworks.
A project in CodeIgniter 4, has the following organization which we will explain in a general way the purpose of each folder and file:
app folder
Inside the app folder, it is where all the application code resides and where we are going to spend most of our time; This folder comes with a default file and folder structure that works well for many applications. The following folders make up the content:
- /Config Stores the configuration files, they are varied, for example, from the database, form validations, emails, filters among others.
- /Controllers Controllers determine the flow of the program; They are one of the three elements of our MVC and it is the one that communicates the view with the model according to the rules that we define.
- /Database Stores the migrations of the database and the seed files (seeds) we talked about this in another chapter.
- /Filters Stores filter-type classes that can be executed before and after handlers you specify.
- /Helpers Help files that store collections of independent functions that we can easily reuse throughout the application.
- /Language This is where we define files to handle the languages of the application.
- /Libraries Useful classes that don't fit into another category; They are more specific schemes than helpers, in which we can rely on classes to create functions that we can reuse.
- /Models The models are nothing more than classes that have a direct relation between the tables with the classes and the columns with the properties of the classes; here we define formats, structures and essentially the communication with the database; this is one of the three elements that correspond to our MVC.
- /ThirdParty Third-party libraries that can be used in applications.
- /Views The views make up the HTML that is displayed to the client and this is another of our MVC layers; it is assigned from the controller.
Folder system
This folder stores the files that make up the framework itself; all the functionality of our framework resides here, therefore the files in the system folder should never be modified. Instead, you can add your functionality via helpers (files with help functions) or create new classes to provide the desired functionality.
All files in this folder are in the "CodeIgniter" namespace.
Folder public
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 that we have with version 3 and it follows the same steps that have been used by other frameworks such as Laravel, which also have this same structure.
Placing a single publicly accessible folder prevents direct access to its 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 meant to be the "web root" of your site, and your web server would be configured to point to it.
Folder writable
This folder contains all the folders and files that might need to be written to over the course of an application's lifetime. This includes files and folders that the framework itself generates, as well as folders to store cache files, logs, and any payloads a user might submit. This allows you to keep your other parent folders write-free as an added security measure to prevent the loading and execution of unwanted code.
Folder tests
This folder is configured to contain your test files.
You do not need to transfer this folder to your production servers.
Folder docs
This folder may or may not be there; It depends on how you get your copy of the framework, it contains a local copy of the CodeIgniter 4 user guide so you know what to do; It is just part of the CodeIgniter website (documentation section) but in pdf version.
- Andrés Cruz
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter