CodeIgniter4: Introduction and Installation
Published:
CodeIgniter 4 is a PHP framework used for building web applications. This framework is lightweight, fast, and easy to learn. In this article, we will discuss the basics of web development using PHP 8, CodeIgniter 4, and how to set up a local development environment using XAMPP.
1. Introduction to PHP 8
PHP (Hypertext Preprocessor) is a server-side programming language widely used for dynamic web development. The latest version, PHP 8, brings a number of improvements and new features that enhance performance, efficiency, and ease of development, including:
- Performance Improvements: With the JIT (Just-In-Time) compiler, code execution becomes significantly faster.
- New Features: Features like union types, attributes, and named arguments improve efficiency and make code maintenance easier.
Here is an example of a simple PHP script inside HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Learning PHP</title>
</head>
<body>
<?php echo "PHP code within HTML"; ?>
</body>
</html>
2. Introduction to CodeIgniter 4
A Web Application Framework (WAF) is a collection of code, libraries, and tools tied together into a framework to speed up the web application development process. CodeIgniter 4 (CI4) is one of them, and some of its advantages include:
- It is free and open-source.
- Relatively small in size, very lightweight, and fast.
- Implements the Model-View-Controller (MVC) design pattern.
- Exceptionally well-documented and beginner-friendly.
Some major updates in CodeIgniter 4 compared to its previous versions are: installation via Composer, a built-in Local Development Server, namespace support following the PSR 4 standard, .env file-based configuration, and a CLI feature named spark.
3. Installing XAMPP
XAMPP is a cross-platform software that provides a localhost server environment. Its components include (X) for Cross-platform, (A) for the Apache web server, (M) for the MySQL/MariaDB database, and (P) for PHP.
- Download XAMPP from apachefriends.org.
- Run the installation process and follow the on-screen instructions.
- Once installed, open the XAMPP Control Panel and click Start for the Apache and MySQL modules.
- Test the server by opening your browser and navigating to
http://localhost.
4. Installing CodeIgniter 4
CodeIgniter 4 can be installed in various ways, but the most recommended method is using Composer.
- Make sure you have Composer installed. You can check its version by running:
composer --version - Navigate your terminal or command prompt to your desired local directory, then run the project creation command:
composer create-project codeigniter4/appstarter project-root(Replace
project-rootwith your desired project directory name, e.g.,project1). - If successful, navigate into the project folder and start the CodeIgniter 4 local server using the following command:
cd project1 php spark serve - Open your browser and go to
http://localhost:8080. If the installation was successful, you will see the “Welcome to CodeIgniter 4” page.
5. CodeIgniter 4 Directory Structure
By default, after a successful installation, you will find several key directories available:
app/: The main directory used to place the application files you will build (source code, views, configurations, etc.).public/: The application’s main entry point (index.php). It enhances security because theappfiles will be hidden from the web server. This is also where assets (like CSS, JS, and images) are stored.tests/: The directory used to store testing scripts for the application.vendor/: The storage directory for third-party libraries or packages installed via Composer.writable/: This directory stores various dynamic files such as cache, logs, and system upload storage.
The /app Sub-directories
File placement inside the app directory is classified according to CodeIgniter conventions. Here is the list of its sub-directories:
Config/: Contains configuration files used by the application.Controllers/: A collection of the application’s control logic (controller) files.Database/: Used for identifying and storing database migrations and seeds.Filters/: Stores filter classes that can run before and after a controller.Helpers/: Used to store a list of self-defined helper files (collections of useful functions).Language/: In this directory, we can define specific constant values in our desired languages.Libraries/: Contains a list of self-defined library files (collections of classes).Models/: A collection of model files required by the application.ThirdParty/: Used to store plugins developed by third parties.Views/: Contains a list of user interface files (views/templates) needed by the application.
6. Model-View-Controller (MVC) Architecture
This architecture separates an application into three main components to make code maintenance easier:
- The
index.phpfile insidepublic/serves as the main entry point. - The Router routes requests to the Controller.
- The Controller interacts with the Model to retrieve or save necessary data.
- After processing the data, the Controller will load the View and pass the data so it can be rendered to the web browser (Web Page).