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

Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
vamsi krishna
 
Web Services - Architecture and SOAP (part 1)
Web Services - Architecture and SOAP (part 1)Web Services - Architecture and SOAP (part 1)
Web Services - Architecture and SOAP (part 1)
Martin Necasky
 

What's hot (20)

Introduction To CodeIgniter
Introduction To CodeIgniterIntroduction To CodeIgniter
Introduction To CodeIgniter
 
Css pseudo-classes
Css pseudo-classesCss pseudo-classes
Css pseudo-classes
 
Flickr
FlickrFlickr
Flickr
 
Siebel 8.1 Certifications Question Answers
Siebel 8.1 Certifications Question AnswersSiebel 8.1 Certifications Question Answers
Siebel 8.1 Certifications Question Answers
 
Frontend
FrontendFrontend
Frontend
 
MVC Architecture
MVC ArchitectureMVC Architecture
MVC Architecture
 
Chapter 15: Floating and Positioning
Chapter 15: Floating and Positioning Chapter 15: Floating and Positioning
Chapter 15: Floating and Positioning
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
 
6 Months SEO Plan for an ecommerce firm
6 Months SEO Plan for an ecommerce firm6 Months SEO Plan for an ecommerce firm
6 Months SEO Plan for an ecommerce firm
 
Web Services - Architecture and SOAP (part 1)
Web Services - Architecture and SOAP (part 1)Web Services - Architecture and SOAP (part 1)
Web Services - Architecture and SOAP (part 1)
 
JDBC
JDBCJDBC
JDBC
 
HTML + CSS Examples
HTML + CSS ExamplesHTML + CSS Examples
HTML + CSS Examples
 
Implicit object.pptx
Implicit object.pptxImplicit object.pptx
Implicit object.pptx
 
HTTP Request Header and HTTP Status Code
HTTP Request Header and HTTP Status CodeHTTP Request Header and HTTP Status Code
HTTP Request Header and HTTP Status Code
 
Answers siebel-set-i
Answers siebel-set-iAnswers siebel-set-i
Answers siebel-set-i
 
Java 17
Java 17Java 17
Java 17
 
Html frames
Html framesHtml frames
Html frames
 
Java bean
Java beanJava bean
Java bean
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Oops in vb
Oops in vbOops in vb
Oops in vb
 

Viewers also liked

Top 100 PHP Questions and Answers
Top 100 PHP Questions and AnswersTop 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
iimjobs and hirist
 

Viewers also liked (6)

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
 

Similar to CodeIgniter 101 Tutorial

LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
Akhil Mittal
 
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
Akhil Mittal
 

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)
 
Code igniter - A brief introduction
Code igniter - A brief introductionCode igniter - A brief introduction
Code igniter - A brief introduction
 
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
 

Recently uploaded

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
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
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
 
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
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
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...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 

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.