Course Content
Introduction to CodeIgniter 4
CodeIgniter is an Application Development Framework. CodeIgniter is a popular and powerful MVC (Model-View-Controller) framework that is used to develop web applications. It is a free and Open-source PHP framework.
0/5
MVC (Model-View-Controller)
MVC stands for Model-View-Controller. MVC is an application design model consisting of three interconnected parts. They include the model (data), the view (user interface), and the controller (processes that handle input).
0/6
Sessions
The Session class allows you to maintain a user’s "state" and track their activity while they browse your site.
0/1
URI Routing
There is a one-to-one relationship between a URL string and its corresponding controller class/method.
0/2
Working with Database
Like any other framework, we need to interact with the database very often and CodeIgniter makes this job easy for us. It provides a rich set of functionalities to interact with the database.
0/5
Spreadsheet
PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Importing Excel and CSV into MySQL help to save the user time and avoid repetitive work.
0/1
CodeIgniter 4 Tutorial

Routing rules are defined in your /app/Config/Routes.php file.

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:

example.com/class/function/id/

Syntax:

$routes->add('from', 'to', $options);

In some cases, you may want to remap this relationship so that a different class/method can be called instead of the one corresponding to the URL.

To learn how to implement routers on a real-world project, we will assume that we are creating an application for managing contact details. The following table shows the URLs that will be working with.

No.URLRouteControllerMethod
1/$route->add(‘Contacts::index’)Contactsindex
2/contacts$route->add(‘Contacts::list’)Contactslist
3/contacts/add$route->add(‘Contacts::contacts/add’)Contactsadd
4/contacts/create$route->post(‘Contacts::contacts/create’)Contactsadd
5/contacts/edit/id$route->get(‘Contacts::contacts/edit/(:num)’)Contactsedit
6/contacts/update$route->post(‘Contacts::contacts/update’)Contactsupdate
7/contacts/delete/id$route->get(‘Contacts::contacts/delete/(:num)’)welcomedelete

 

Create URLs for Applications

create routes for our project. Open /app/Config/Routes.php

$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->add('/', 'Contacts::index');
$routes->add('contacts/list', 'Contacts::list');
$routes->add('contacts/add', 'Contacts::add');
$routes->post('contacts/create', 'Contacts::create');
$routes->get('contacts/edit/(:any)', 'Contacts::edit/$1');
$routes->post('contacts/update', 'Contacts::update');
$routes->get('contacts/delete/(:any)', 'Contacts::delete/$1');

The following table shows the respective URLs obtained from the above-defined routes

No.RouteURL
1$routes->add(‘/’, ‘Contacts::index’);http://localhost
2$routes->add(‘contacts/list’, ‘Contacts::list’)http://localhost/contacts/list
3$routes->add(‘contacts/add’, ‘Contacts::add’);http://localhost/contacts/add
3$routes->post(‘contacts/create’, ‘Contacts::create’);http://localhost/contacts/create
4$routes->get(‘contacts/edit/(:any)’, ‘Contacts::edit/$1’);http://localhost/contacts/edit/1
5$routes->post(‘contacts/update’, ‘Contacts::update’);http://localhost/contacts/update/1
6$routes->get(‘contacts/delete/(:any)’, ‘Contacts::delete/$1’);http://localhost/contacts/delete/1

 

Directory structure

/app
Views
	+ contacts
		- add.php
		- list.php
		- edit.php

	+ innerpages
		- header.php
		- footer.php
		- template.php
	- home.php

 

Global Options

The methods for creating a route (add, get, post, resource, etc.) can take an array of options that can modify the generated routes or further restrict them. The $options an array is always the last parameter:

$routes->get('from', 'to', $options);
$routes->post('from', 'to', $options);
$routes->put('from', 'to', $options);
$routes->head('from', 'to', $options);
$routes->options('from', 'to', $options);
$routes->delete('from', 'to', $options);
$routes->patch('from', 'to', $options);
$routes->match(['get', 'put'], 'from', 'to', $options);
$routes->resource('photos', $options);
$routes->map($array, $options);
$routes->group('name', $options, function());