Table Of Content
- Environment Configuration
- Database and Table Creation
- Connecting Database
- Start Web Server
- CodeIgniter Route
- How to Create an Application Model
- How to Create an Application Controller and CRUD Methods
- Show a Listing of Records and Edit and Delete Action
- Create an Add Employee Page
- Edit Form of the Particular Record
- That’s all with the CodeIgniter 4 CRUD Operation
CodeIgniter is a PHP framework that is used to develop web applications. Versions 2 and 3 of CodeIgniter are significantly different from version 4. Only PHP versions 7.2 and later are supported by CodeIgniter 4. If your server does not support the minimum required version of PHP, you should upgrade your PHP version prior to installation and configuration.
CodeIgniter 4 cannot run in a case where your server does not have any of these extensions enabled, i.e
php-xml so please check your
php.ini file and enable these all if it is not.
In this post, we shall examine the basic installation, configuration, and implementation of CRUD operations as well as the route configuration.
Please make sure the composer is installed. No worries if it is not installed, you can download the composer from the official website
You can check commands based on your OS.
To create a CodeIgniter 4 setup, run this given command into your shell or terminal.
$ composer create-project codeigniter4/appstarter Codeigniter-4
Assuming you have successfully installed an application on your local system.
Now, let’s configure the database and application connectivity.
Here, we need to work with the env file for the global variables.
When we install CodeIgniter 4, we will have an env file at the root. To use the environment variables, we need to do
.env so we can simply rename the file. There is also a command to copy the env file and it will create a new file with it. env, rename the file. Make sure you are in your project directory.
$ cp env .env
Now you are good to go and can use the
.env file for the global variables.
There are two environment modes in CodeIgniter, so when we are installing CodeIgniter 4, it starts up in production mode by default. We need to make it in development mode so it will show all the errors on-screen if there are any on the page.
.env file from root.
Now the application is in development mode as it is installed on your local server.
-> Note: Please remove the
# of your env variable that you are going to use, otherwise, env is considered as a commented variable.
Database and Table Creation
As usual, we can create a database using the PhpMyAdmin tool OR alternatively by executing commands for the same.
CREATE DATABASE mydb_ci4_app;
The database was successfully created. Now let’s create some required tables for our application to store data.
The table was created successfully. Now it is time to connect the database to our application.
As mentioned above,
.env the file is responsible for global variables, so database connection variables need to be set up in the
.env file that is located in your project root directory.
.env file and search for the “database” keyword. You can see the database configuration variables, so update these variables with your details.
Your database is connected successfully with your web application.
Start Web Server
We just need to execute one command to start the application server.
php spark serve
It will create your localhost application URL like
Typically, there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
CodeIgniter Routing matches the URL to the predefined routes. If no route match is found, then CodeIgniter throws a page not found exception. Controllers – routes are linked to controllers. Controllers glue the models and views together.
If outing rules are defined in your
/app/Config/Routes.php file, they should be similar to the examples below.
You can see some important terms, match, get, post, and filter, so all those are checkers of your URL request and if matches are found then only it will allow you to proceed.
Filters check if the particular route needs authentication or not, so if a particular page user must log in, we just need to add a filter as auth and vice versa. We will check later in the next session on how to create and configure the
filters in our application, so as of now you can follow the steps without the filter option.
How to Create an Application Model
Models are PHP classes that are designed to work with information in your database.
Models are created and found in the
/app/Models directory of your application, so let’s create an employee model.
To create a model, we just need to execute a simple command and it will create the model in the particular directory.
$ php spark make:model Employee --suffix
-> Note: In case you have created a model using the command, please check the model variables as per your table.
How to Create an Application Controller and CRUD Methods
Controllers are created and found in the
/app/Controllers directory of your application, so let’s create an employee controller.
To define the route of the implemented controller for the CRUD operation,
As mentioned above, we need to define all routes in the
/app/Config/Route.php so these are the required routes needed to manage the CRUD operation.
Now, let’s check with the employee listing view with the basic layout and table heading and rows.
Show a Listing of Records and Edit and Delete action
We need to create a view file for listing, editing, and adding pages along with the related form fields.
View files reside in the
app/Views so here we are going to create our employee directory inside the Views and will create employee section-related view files only inside the employee, so our directory is app/Views/employee for the listing page we are going to create
This page layout is using bootstrap CSS and without client-side validations. We will check the client and server-side validations in the next article along with the data table one-time listing as well as the server-side requests.
Create an Add Employee Page
We need to use the same directory path as we created our listing page, so let’s create an
add.php file for the create new employee record form.
Let’s go for the edit action of the particular record, so we need to create a new file inside the same directory i.e
app/Views/employee and our file name is
Edit Form of the Particular Record
And as we have defined the delete action, it will call the matched route and will delete the particular record.
That’s all with the CodeIgniter 4 CRUD operation
In the end, we were successful in completing the CodeIgniter 4 CRUD operation lesson. In this tutorial, we have started from the very beginning by putting everything in its appropriate place and going through all of the fundamentals.
If there is still a chance that you may be uncertain about the whole process, so hire a CodeIgniter developer to assist you.