CodeIgniter 4 is the latest version right now. Because codeIgniter4 is completely different from its previous versions. CodeIgniter 4 completely have changed it’s directory structure. And there are lot of different things you can see in codeIgniter 4 now. Well, I am going to discuss about the directory structure in codeIgniter 4.
Usually we do not use all of them but we can use and We will discover together all the directories and their reponsiblities.
Basically, In app directory we will have some other directories with we used to work directly. Well, The main stuff is there under App directory. Like controllers, Models ,views and some other important stuff also.
Config means configuration, and that means this directory contains all the configuration files. The files that stay under config directory basically work globally. Because it has our database file, app file and some important files like routes.php so these are files that we use mostly. Some other files I would explain below.
In app file you will have some core stuff defined about application like base url, session driver, main index file name and so on.
This file defines the namespaces and class maps so the Autoloader can find the files as needed.
When you use cache in your application then you might need this file. It has some important configurations like file handler for cache and some other that are directly related to chache.
In this file you can define constants and it also have some constants already defined and more importantly you can use these constants globally.
You can read about CSP. Stores the default settings for the ContentSecurityPolicy.
set cookies and you can set some interesting stuff about cookies in this file like expiration time, domain and prefix if you want to add.
Basically, CodeIgniter4 provides a library specifically for CURL. You can use CURL libary and this file contains one property that depends on you if you will make shareOpions false and you will send request multiple time with same class instance then it create errors. So, it only provides shareOption if you will set it to false then it will not occur errors. But you can install the library and enjoy using CURLReqeust Library.
Database file contains database configuration.
List of valid document types.
This is base class for sending mails.
Class used for Encryptions.
Events are cool part of any framework because without going into the running process you can fire any actions. For example if you are publishing a post and you want to send mail for each model in the same way you can create an event that will be firing email on each step. You can read about events on CI4
Setup how the exception handler works
Enable/disable backward compatibility breaking features.
Filter is something that I have personally used in Authentication. It has a property that called $globals and it has two keys which works after and before of each request.
Describe the foreign characters for transliteration with text helper.
Available Response Formats. A Factory method to return the appropriate formatter for the given mime type.
If you don’t know you can create your own commands in CodeIgniter 4 and Generator.php helps you to list down the views or you can modify them too. Other things you can read comments in file that describes it in better way.
The Honeypot Class makes it possible to determine when a Bot makes a request to a CodeIgniter4 application, if it’s enabled in Application\Config\Filters.php file.
CodeIgniter’s Image Manipulation class lets you perform the following actions:
- Image Resizing
- Thumbnail Creation
- Image Cropping
- Image Rotating
- Image Watermarking
Kint for PHP is a tool designed to present your debugging data in the absolutely best way possible.
All about logging information. It has handle which are depends on the bug you find in app. So, you can basically read some core stuff about logging info in this file.
Migrations are by default enabled. This contains some properties that directly interact with migration. Migration used to create database tables using codeigniter4 inbuilt schema’s.
This file contains an array of mime types. It is used by the Upload class to help identify allowed file types.
Enable Auto-Discovery? Giving a slight performance boost
Pager class helps to create pagination on page and basically it’s inbuilt class that contains view names and some other stuff that helps to build pagination when rendering that data on view.
Holds the paths that are used by the system to locate the main directories, app, system, etc.
Defines basic security restrictions for the Publisher class to prevent abuse by injecting malicious files into a project
Routes directory Contains Core CodeIgniter stuff about Routing.
Protection Method for Cross Site Request Forgery protection.
This file holds any application-specific services.
List of toolbar collectors that will be called when Debug Toolbar fires up and collects data from.
This file contains four arrays of user agent data. It is used by the User Agent Class to help identify browser, platform, robot, and mobile device data.
Contains the class
This file stores some properties that are responsible for some actions like filter that you use while rendering data. Helps to reduce the potential abuse.
In controllers you can store your controllers files and you can create controller with cli using php spark make:controller controllername command.
There is a file in controller which is BaseController.php you can define autoloader files like helper and services, in its method called initcontroller.
Database contains two directories first one will be migrations and second will be seeds in which you can create database schema files to create table and seed ( insert ) bulk data into tables.
As name defines, filter directory contains all filter files which checks each request and you can pass any piece of code in before and after method. Before method checks the provided code before any incoming request and same goes for after method. I usually use it for checking authenticated users.
Helpers directory contains all the helper files. In helper directory you can create custom helpers files. That will be available across all over the application. Usually I use helpers to create common function.
You can call helpers using helper(‘ ‘) method.
Tip : If you are building any helper file and want to use them in your controller files. Then use helper once in initController method of BaseController File.
In libraries you can create custom libraries.
When we create a custom library in CodeIgniter 4, It will be stored into /app/Libraries directory.
As we have already discussed that libraries are classes. These classes are created for some specific functions.
In models directory you can create Models. Models basically used to interact with single table of a database. In CodeIgniter 4 there is very convenient way to interact with the model and query builder in one of the best thing to make CRUD requests.
You can create Model using php spark make:model modelname command.
If you are building any app using codeigniter 4 then you might need third party libraries like to generate pdf and excel report etc..
In Views directory contains all the frontend files of your application. And If you are going to build any application in ci4 then don’t forget to use templating way of CodeIgniter to manage different parts of your view.
I hope this will help you to understand the structure of codeIgniter 4.
PHP Laravel |Full Stack Developer | Technical Writer