SlideShare a Scribd company logo
1 of 17
Basic Tutorial
Index
This is a tutorial with basic
instructions about CodeIgniter.



It’s purpose is to help someone
with no prior knowledge of
frameworks, to understand it’s
basic principles and how it
works.









Index
About Frameworks
About MVC
Intro to CodeIgniter
Controllers
Routes File
Models
Frameworks
Frameworks are abstract, reusable platforms where we can develop
our applications.

They help in writing reusable and better constructed code.
Their main characteristic is the MVC (Model – View – Control)
architecture pattern.
MVC
The MVC architecture pattern separates the representation
of data from the logic of the application.
The View is what the visitors of the web application see.
The Controller is responsible for handling the incoming
requests, validating input and showing the right view.
The Model is responsible for accessing the database or
executing other operations.
MVC
Controller
Gets incoming requests.
Displays the right View.
Communicates with the
model.

The Controller calls
the Model.

Points to a View and
carries data.
Triggers a Controller

The Model retrieves / creates the
data asked and returns them to the
Controller
Model
Queries the database.
Executes operations.
Gives data to controller.

View
What the visitors see.
Html pages with PHP.
Displays data from
Controller
What is CodeIgniter
CodeIgniter is a PHP framework, easy to learn, suitable for
beginners.
 It needs no extra configuration.
 You do not need to use the command line.

 It is extremely light.
 You don’t need to learn a templating language.
 It is suitable for small or big projects.

All in all, you just need to know some PHP to develop the
applications you want.
Intro to CodeIgniter
How does a Controller trigger?
Each Controller triggers by a certain URI.
What happens when a Controller is triggered?
It then calls a Model (if any data should be retrieved or created),
it finds the View that should be shown to the visitor and then it
returns that View with the corresponding data.
How does CI knows what Controller to trigger?
This is defined by routes. Routes is a PHP configuration file that
maps each URL of our web project to a Controller and a certain
function.
Intro to CodeIgniter
User enters the URI of
the project

CI gets the request and
checks the routes file to
find any matches.

If a match is found, it
triggers the right
Controller and function.

View and data is
represented to
the user.

After the data is retrieved
the Controller finds the
right View and returns it.

The Controller calls the
right Model to retrieve /
create the data needed.
Controllers – The Class
A controller in CI is basically a custom made class, which inherits from the
CI_Controller class.

class WELCOME extends CI_Controller {
}

Create a controller with name
“welcome.php”. The new
controller is located into
applications/controllers folder.

In every controller we create, the constructor method must be
declared. In the constructor we can load libraries, models, the
database, create session variables and generally define content that
will be used by all methods. The constructor must have the same
name as the class.
Controllers – The Constructor
public function WELCOME() {
parent::_construct();

The first function of the controller is
the constructor, where we can load…

$this->load->helper(‘url’);
$this->load->helper(‘file’);

… the helper libraries we may
need….

$this->load->database();

… the database of our project…

$this->load->model(‘welcome_model’);
}

… and the models we need.
Controllers – The Functions
Then we can write the functions of the controller, that will be triggered by
the system. The functions can either perform some operations (through
the model), load a view, or even both.

public function home () {
if (!file_exists(application/views/home.php)){
show_404();
}
$this->load->view(‘home’);
}

The most usual operation is
to check if a view file exists
and load it.
View files are located in
application/views folder.
Views are what the visitors
see.
In case it does not exist, a
404 error message is
displayed to the visitor.
Controllers – The Functions
public function home () {
if (!file_exists(application/views/home.php)){
show_404();
}
$data[‘files’]=$this->welcome_model->get_data();
$this->load->view(‘home’,$data);
}

In case we need to send some data to the view, we retrieve them by calling the right
function from the model and then load them with the view. In the above example, the
data is then accessible by the name “files” in the “home” view.
Routes File
The routes file contains the matches for the URIs and the Controllers /
Functions.
There is a default controller which is triggered from the Base URL of
our project (e.g. www.my_project.com).
$route[„default_controller‟]=“controller_name/function_name”;
So, if we try to access the www.my_project.com, the routes file
understands it as the default controller and triggers the controller and
function we define, e.g. the welcome controller and the home function.
$route[„default_controller‟]=“welcome/home”;
Routes File
There is a pattern which we have to follow in order to create mappings of
URIs and controllers / functions.
my_project.com/class/function/id/
The first segment is reserved for the controller class, the second for the
function and the third of any values we want to pass as arguments
(optional). In case we don’t want to follow this pattern, the URI handler
has to be reconfigured.
If we want to map another URI, we have to follow that pattern. In the
following example if the URI is www.my_project.com/welcome/blog, CI
triggers the welcome controller and the blog method.
$route[„welcome/blog‟]=“welcome/blog”;
Models – Class/ Constructor
In a model we perform some tasks such as execute database queries, read / write
files or perform other operations. The models are located in applications/models
folder.

class Welcome_model extends CI_Model {

Each model we create, extends from
the CI_Model.

public function _construct () {
$this->load->database();
}

At first we have to write the
constructor of the model. In the
constructor we load the database or
other helper libraries.

}
Models - Functions
class Welcome_model extends CI_Model {
public function get_data() {
$query=$this->db->get(….);
/* perform other operations */
return $files;
}
}

In a model function we can perform
any operation we need, and then
return the data to the corresponding
function in the controller.
End of tutorial
The previous topics complete a basic intro into CodeIgniter and how it
essentially works.
CodeIgniter supports helpers, which is essentially a collection of functions
in a category, for example the helper for working with files (read / write) is
“file” and libraries as form validation. All of these can come in handy and
help a lot in developing your projects.
The database class of CodeIgniter supports both traditional structures as
Active Records patterns. Also, someone could set up CodeIgniter to run
with Doctrine (ORM), a topic that will be presented in another tutorial.
For the complete CodeIgniter documentation visit here.
Thank you for reading my tutorial.

More Related Content

What's hot (20)

Introduction To CodeIgniter
Introduction To CodeIgniterIntroduction To CodeIgniter
Introduction To CodeIgniter
 
jQuery
jQueryjQuery
jQuery
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Html TAGS
Html TAGSHtml TAGS
Html TAGS
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
Dom
Dom Dom
Dom
 
Jquery
JqueryJquery
Jquery
 
Introduction to django
Introduction to djangoIntroduction to django
Introduction to django
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2Routing & Navigating Pages in Angular 2
Routing & Navigating Pages in Angular 2
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Ajax Ppt
Ajax PptAjax Ppt
Ajax Ppt
 
Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 

Viewers also liked

Building RESTtful services in MEAN
Building RESTtful services in MEANBuilding RESTtful services in MEAN
Building RESTtful services in MEANMadhukara Phatak
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and AnswersTop 100 PHP Questions and Answers
Top 100 PHP Questions and Answersiimjobs and hirist
 
Advanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JSAdvanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JSSimon Guest
 
Mysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sqlMysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sqlAimal Miakhel
 

Viewers also liked (7)

Building RESTtful services in MEAN
Building RESTtful services in MEANBuilding RESTtful services in MEAN
Building RESTtful services in MEAN
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and AnswersTop 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
 
Advanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JSAdvanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JS
 
Javascript Best Practices
Javascript Best PracticesJavascript Best Practices
Javascript Best Practices
 
Mysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sqlMysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sql
 
PHP - Introduction to PHP MySQL Joins and SQL Functions
PHP -  Introduction to PHP MySQL Joins and SQL FunctionsPHP -  Introduction to PHP MySQL Joins and SQL Functions
PHP - Introduction to PHP MySQL Joins and SQL Functions
 
Codeigniter
CodeigniterCodeigniter
Codeigniter
 

Similar to CodeIgniter 101 Tutorial

LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLAkhil Mittal
 
Mvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaMvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaalifha12
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desaijinaldesailive
 
Get things done with Yii - quickly build webapplications
Get things done with Yii - quickly build webapplicationsGet things done with Yii - quickly build webapplications
Get things done with Yii - quickly build webapplicationsGiuliano Iacobelli
 
Creating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JSCreating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JSAkshay Mathur
 
Folio3 - An Introduction to PHP Yii
Folio3 - An Introduction to PHP YiiFolio3 - An Introduction to PHP Yii
Folio3 - An Introduction to PHP YiiFolio3 Software
 
IRJET- Lightweight MVC Framework in PHP
IRJET- Lightweight MVC Framework in PHPIRJET- Lightweight MVC Framework in PHP
IRJET- Lightweight MVC Framework in PHPIRJET Journal
 
Principles of MVC for Rails Developers
Principles of MVC for Rails DevelopersPrinciples of MVC for Rails Developers
Principles of MVC for Rails DevelopersEdureka!
 
Sitecore MVC (User Group Conference, May 23rd 2014)
Sitecore MVC (User Group Conference, May 23rd 2014)Sitecore MVC (User Group Conference, May 23rd 2014)
Sitecore MVC (User Group Conference, May 23rd 2014)Ruud van Falier
 
How to Create and Load Model in Laravel
How to Create and Load Model in LaravelHow to Create and Load Model in Laravel
How to Create and Load Model in LaravelYogesh singh
 
AspMVC4 start101
AspMVC4 start101AspMVC4 start101
AspMVC4 start101Rich Helton
 
Sitecore MVC (London User Group, April 29th 2014)
Sitecore MVC (London User Group, April 29th 2014)Sitecore MVC (London User Group, April 29th 2014)
Sitecore MVC (London User Group, April 29th 2014)Ruud van Falier
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkAkhil Mittal
 
Codeigniter simple explanation
Codeigniter simple explanation Codeigniter simple explanation
Codeigniter simple explanation Arumugam P
 

Similar to CodeIgniter 101 Tutorial (20)

LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
 
Mvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaMvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senja
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
 
Get things done with Yii - quickly build webapplications
Get things done with Yii - quickly build webapplicationsGet things done with Yii - quickly build webapplications
Get things done with Yii - quickly build webapplications
 
Creating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JSCreating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JS
 
Jinal desai .net
Jinal desai .netJinal desai .net
Jinal desai .net
 
Folio3 - An Introduction to PHP Yii
Folio3 - An Introduction to PHP YiiFolio3 - An Introduction to PHP Yii
Folio3 - An Introduction to PHP Yii
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
MVC in PHP
MVC in PHPMVC in PHP
MVC in PHP
 
IRJET- Lightweight MVC Framework in PHP
IRJET- Lightweight MVC Framework in PHPIRJET- Lightweight MVC Framework in PHP
IRJET- Lightweight MVC Framework in PHP
 
Principles of MVC for Rails Developers
Principles of MVC for Rails DevelopersPrinciples of MVC for Rails Developers
Principles of MVC for Rails Developers
 
MVC 4
MVC 4MVC 4
MVC 4
 
Yii php framework_honey
Yii php framework_honeyYii php framework_honey
Yii php framework_honey
 
Sitecore MVC (User Group Conference, May 23rd 2014)
Sitecore MVC (User Group Conference, May 23rd 2014)Sitecore MVC (User Group Conference, May 23rd 2014)
Sitecore MVC (User Group Conference, May 23rd 2014)
 
How to Create and Load Model in Laravel
How to Create and Load Model in LaravelHow to Create and Load Model in Laravel
How to Create and Load Model in Laravel
 
Rails notification
Rails notificationRails notification
Rails notification
 
AspMVC4 start101
AspMVC4 start101AspMVC4 start101
AspMVC4 start101
 
Sitecore MVC (London User Group, April 29th 2014)
Sitecore MVC (London User Group, April 29th 2014)Sitecore MVC (London User Group, April 29th 2014)
Sitecore MVC (London User Group, April 29th 2014)
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
 
Codeigniter simple explanation
Codeigniter simple explanation Codeigniter simple explanation
Codeigniter simple explanation
 

Recently uploaded

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 

Recently uploaded (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

CodeIgniter 101 Tutorial

  • 2. Index This is a tutorial with basic instructions about CodeIgniter.  It’s purpose is to help someone with no prior knowledge of frameworks, to understand it’s basic principles and how it works.      Index About Frameworks About MVC Intro to CodeIgniter Controllers Routes File Models
  • 3. Frameworks Frameworks are abstract, reusable platforms where we can develop our applications. They help in writing reusable and better constructed code. Their main characteristic is the MVC (Model – View – Control) architecture pattern.
  • 4. MVC The MVC architecture pattern separates the representation of data from the logic of the application. The View is what the visitors of the web application see. The Controller is responsible for handling the incoming requests, validating input and showing the right view. The Model is responsible for accessing the database or executing other operations.
  • 5. MVC Controller Gets incoming requests. Displays the right View. Communicates with the model. The Controller calls the Model. Points to a View and carries data. Triggers a Controller The Model retrieves / creates the data asked and returns them to the Controller Model Queries the database. Executes operations. Gives data to controller. View What the visitors see. Html pages with PHP. Displays data from Controller
  • 6. What is CodeIgniter CodeIgniter is a PHP framework, easy to learn, suitable for beginners.  It needs no extra configuration.  You do not need to use the command line.  It is extremely light.  You don’t need to learn a templating language.  It is suitable for small or big projects. All in all, you just need to know some PHP to develop the applications you want.
  • 7. Intro to CodeIgniter How does a Controller trigger? Each Controller triggers by a certain URI. What happens when a Controller is triggered? It then calls a Model (if any data should be retrieved or created), it finds the View that should be shown to the visitor and then it returns that View with the corresponding data. How does CI knows what Controller to trigger? This is defined by routes. Routes is a PHP configuration file that maps each URL of our web project to a Controller and a certain function.
  • 8. Intro to CodeIgniter User enters the URI of the project CI gets the request and checks the routes file to find any matches. If a match is found, it triggers the right Controller and function. View and data is represented to the user. After the data is retrieved the Controller finds the right View and returns it. The Controller calls the right Model to retrieve / create the data needed.
  • 9. Controllers – The Class A controller in CI is basically a custom made class, which inherits from the CI_Controller class. class WELCOME extends CI_Controller { } Create a controller with name “welcome.php”. The new controller is located into applications/controllers folder. In every controller we create, the constructor method must be declared. In the constructor we can load libraries, models, the database, create session variables and generally define content that will be used by all methods. The constructor must have the same name as the class.
  • 10. Controllers – The Constructor public function WELCOME() { parent::_construct(); The first function of the controller is the constructor, where we can load… $this->load->helper(‘url’); $this->load->helper(‘file’); … the helper libraries we may need…. $this->load->database(); … the database of our project… $this->load->model(‘welcome_model’); } … and the models we need.
  • 11. Controllers – The Functions Then we can write the functions of the controller, that will be triggered by the system. The functions can either perform some operations (through the model), load a view, or even both. public function home () { if (!file_exists(application/views/home.php)){ show_404(); } $this->load->view(‘home’); } The most usual operation is to check if a view file exists and load it. View files are located in application/views folder. Views are what the visitors see. In case it does not exist, a 404 error message is displayed to the visitor.
  • 12. Controllers – The Functions public function home () { if (!file_exists(application/views/home.php)){ show_404(); } $data[‘files’]=$this->welcome_model->get_data(); $this->load->view(‘home’,$data); } In case we need to send some data to the view, we retrieve them by calling the right function from the model and then load them with the view. In the above example, the data is then accessible by the name “files” in the “home” view.
  • 13. Routes File The routes file contains the matches for the URIs and the Controllers / Functions. There is a default controller which is triggered from the Base URL of our project (e.g. www.my_project.com). $route[„default_controller‟]=“controller_name/function_name”; So, if we try to access the www.my_project.com, the routes file understands it as the default controller and triggers the controller and function we define, e.g. the welcome controller and the home function. $route[„default_controller‟]=“welcome/home”;
  • 14. Routes File There is a pattern which we have to follow in order to create mappings of URIs and controllers / functions. my_project.com/class/function/id/ The first segment is reserved for the controller class, the second for the function and the third of any values we want to pass as arguments (optional). In case we don’t want to follow this pattern, the URI handler has to be reconfigured. If we want to map another URI, we have to follow that pattern. In the following example if the URI is www.my_project.com/welcome/blog, CI triggers the welcome controller and the blog method. $route[„welcome/blog‟]=“welcome/blog”;
  • 15. Models – Class/ Constructor In a model we perform some tasks such as execute database queries, read / write files or perform other operations. The models are located in applications/models folder. class Welcome_model extends CI_Model { Each model we create, extends from the CI_Model. public function _construct () { $this->load->database(); } At first we have to write the constructor of the model. In the constructor we load the database or other helper libraries. }
  • 16. Models - Functions class Welcome_model extends CI_Model { public function get_data() { $query=$this->db->get(….); /* perform other operations */ return $files; } } In a model function we can perform any operation we need, and then return the data to the corresponding function in the controller.
  • 17. End of tutorial The previous topics complete a basic intro into CodeIgniter and how it essentially works. CodeIgniter supports helpers, which is essentially a collection of functions in a category, for example the helper for working with files (read / write) is “file” and libraries as form validation. All of these can come in handy and help a lot in developing your projects. The database class of CodeIgniter supports both traditional structures as Active Records patterns. Also, someone could set up CodeIgniter to run with Doctrine (ORM), a topic that will be presented in another tutorial. For the complete CodeIgniter documentation visit here. Thank you for reading my tutorial.