Directory Structure In CodeIgniter 4 | CodeIgniter4

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.

Directory Structure In CodeIgniter 4 | CodeIgniter4

App Directory

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 Directory

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.

App.php

In app file you will have some core stuff defined about application like base url, session driver, main index file name and so on.

Autolaod.php

This file defines the namespaces and class maps so the Autoloader can find the files as needed.

Cache.php

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.

Constants.php

In this file you can define constants and it also have some constants already defined and more importantly you can use these constants globally.

ContentSecurityPolicy.php

You can read about CSP. Stores the default settings for the ContentSecurityPolicy.

Cookie.php

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.

CurlRequest.php

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.php

Database file contains database configuration.

DocTypes.php

List of valid document types.

Email.php

This is base class for sending mails.

Encryption.php

Class used for Encryptions.

Events.php

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

Exceptions.php

Setup how the exception handler works

Features.php

Enable/disable backward compatibility breaking features.

Filters.php

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.

ForiegnCharacters.php

Describe the foreign characters for transliteration with text helper.

Format.php

Available Response Formats. A Factory method to return the appropriate formatter for the given mime type.

Generator.php

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.

Honeypot.php

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.

Images.php

CodeIgniter’s Image Manipulation class lets you perform the following actions:

  • Image Resizing
  • Thumbnail Creation
  • Image Cropping
  • Image Rotating
  • Image Watermarking

Kint.php

Kint for PHP is a tool designed to present your debugging data in the absolutely best way possible.

Logger.php

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.php

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.

Mimes.php

This file contains an array of mime types.  It is used by the Upload class to help identify allowed file types.

Modules.php

Enable Auto-Discovery? Giving a slight performance boost

Pager.php

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.

Paths.php

Holds the paths that are used by the system to locate the main directories, app, system, etc.

Publisher.php

Defines basic security restrictions for the Publisher class to prevent abuse by injecting malicious files into a project

Routes.php

Routes directory Contains Core CodeIgniter stuff about Routing.

Security.php

Protection Method for Cross Site Request Forgery protection.

Services.php

This file holds any application-specific services.

Toolbar.php

List of toolbar collectors that will be called when Debug Toolbar fires up and collects data from.

UserAgents.php.

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.

Validation.php

Contains the class

View.php

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.

Controllers

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

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.

Filters

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

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.

Libraries

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.

Models

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.

Third Party

If you are building any app using codeigniter 4 then you might need third party libraries like to generate pdf and excel report etc..

Views

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.

Conclusion

I hope this will help you to understand the structure of codeIgniter 4.