ghConnectHub

CodeIgniter 4 | Choosing a faster PHP framework

John Kreativ |
Technology & Gadgets
Is PHP the most relevant and widely adopted language used in server side development today? Codeigniter, a product initially developed by Ellislab, subsequently by Columbia Institute of Technology and now a community maintain project.

CodeIgniter was developed to make PHP projects simple, flexible and fast, also provides developers with a rich set of libraries for routine tasks

With CodeIgniter's flexibility, novices and advanced users can work in the way they want, not being forced into working in a restrictive way. This PHP framework can have core parts easily extended or completely replaced to make the system work the way you need it to.

CodeIgniter framework stands tall amongst it peers like Laravel and Symphony.

What is a PHP Framework?

The PHP coding language is still used for nearly 80% of all websites in 2023. it is extremely robust
A PHP framework is built on the open-source language PHP and offers built-in features to improve your software development lifecycle. These features increase development speed and improve performance within your team by lowering the workload of your developers.

CodeIgniter 4 (CI 4) directories

A fresh install has five directories: app/, public/, writable/, tests/ and vendor/ or system/. Each of these directories has a very specific part to play.

app/

Config/ Stores the configuration files
Controllers/ Controllers determine the program flow
Database/ Stores the database migrations and seeds files
Filters/ Stores filter classes that can run before and after controller
Helpers/ Helpers store collections of standalone functions
Language/ Multiple language support reads the language strings from here
Libraries/ Useful classes that don't fit in another category
Models/ Models work with the database to represent the business entities
ThirdParty/ ThirdParty libraries that can be used in application
Views/ Views make up the HTML that is displayed to the client

What is MVC Architecture

A Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application. MVC has become one of the most frequently used industry-standard development framework on the web used to create scalable and extensible projects

MVC Structure

Codeigniter is popular because of its MVC structure. This structure makes it easy to write clean and simple code. It also allows a developer to isolate parts of their code that they don’t want users to access. The model-view-Controller architecture gives developers total control over how their applications work.

The code generated by CodeIgniter comes with a well-designed Model-View-Controller architecture, which encourages good programming practices, including modularity and reuse. The model layer provides data access, validation, querying, caching, etc. where the view layer displays data to the user. The controller acts as an interface between models and views. It handles requests from the browser or other interfaces, interprets them, and directs them to the appropriate place.

Understanding Models, Views & Controllers

Models manage the data of the application and help to enforce any special business rules the application might need.

A model’s job is to maintain a single type of data for the application. This might be users, blog posts, transactions, etc. In this case, the model’s job has two parts: enforce business rules on the data as it is pulled from, or put into, the database; and handle the actual saving and retrieval of the data from the database.

Views are simple files, with little to no logic, that display the information to the user, thus handles the user interface (UI).
They are typically HTML with very small amounts of PHP. The PHP should be very simple, usually just displaying a variable’s contents, or looping over some items and displaying their information in a table.

Views get the data to display from the controllers, who pass it to the views as variables that can be displayed with simple echo calls. You can also display other views within a view, making it pretty simple to display a common header or footer on every page.

Controllers act as an interface between Model and View components to process all the business logic and incoming requests, manipulate data using the Model component and interact with the Views to render the final output

Controllers have a couple of different roles to play. The most obvious one is that they receive input from the user and then determine what to do with it. This often involves passing the data to a model to save it, or requesting data from the model that is then passed on to the view to be displayed. This also includes loading up other utility classes, if needed, to handle specialized tasks that is outside of the purview of the model.

Is CodeIgniter 4 backward compatible with CodeIgniter 3?

CodeIgniter 4 is a rewrite of the framework and is not backwards compatible. It is more appropriate to think of converting your app, rather than upgrading it. Once you have done that, upgrading from one version of CodeIgniter 4 to the next will be straightforward.

How to Install CodeIgniter 4

CodeIgniter 4 can be installed automatically via the use of Composer or manual download.

Installing CodeIgniter 4 using Composer

You need a webserver to install CodeIgniter in the public_html (root directory).
1. Composer create-project codeigniter4/appstarter project_root_name
2. Composer update -to get the latest version of CodeIgniter
3. pwd - to get the directory name

Manual install of CodeIgniter

1. Download the latest version of CodeIgniter
2. Extract the contents and move to your webserver public_html, thus root folder
3. In the browser of your choice, enter the url: https://yourdomain.com/public
4. If you see CodeIgniter's default screen, then you have successfully installed it.

Removing the suffix public in codeIgniter's url

Removing public in the url can be done in two (2) ways:

Method 1:

a. Move the .htaccess file and index.php file within the app/public folder of codeIgniter to the /app folder of codeIgniter
b. Open the index.php file that was moved to the /app folder and look for FCPATH, remove the two dots.

Method 2: Create a file and save it with .htaccess extension then paste the following:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.x)$ public/$1 [L]

Why Choose CodeIgniter 4?

PHP is the most used server side language used on the world wide web and powers wordPress website. its dominance is not going to change anytime from now. If you and in search for a server side programming language which is fast, easy to learn, wide documentations and help, then you should consider a PHP framework like CodeIgniter

Drawbacks of CI Framework

There are a few drawbacks to using a PHP framework for your software development teams.
The first is an increase in programming restrictions. You can do far more with PHP if you don’t use a framework. A framework will reduce programming complexity. This works well if you’re looking for efficiency. However, manual coding gives your more flexibility in what you can accomplish.

Additionally, each framework has a learning curve. You won’t immediately know how to interact and use that specific framework. This learning curve is not nearly as difficult as learning an entirely new programming language, but you’ll still need time to get up to speed.

Why Choose CodeIgniter 4?

1. Easy to Detect and Hand Errors
2. MVC Structure
3. Built-in Libraries and Helper Functions
4. Easy Customization and Configuration
5. Namespace and autoloading
6. Enhanced Security Features
7. Versatile Database Migration Tools
8. Great community support
9. Better Development and SEO
10. Small Footprint to Download
11. Using MVC is not a compulsion
12. Works on any PHP version