You are on page 1of 22

Introduction to

CodeIgniter

Why use a framework?

Web application frameworks provide basic


building blocks needed by most applications

Database connections
Business logic
Form handling

Separation of concerns
Easier testing (unit tests)

Framework Popularity 2013-2014

Framework Popularity 2015

What is CodeIgniter?

CodeIgniter is a lightweight web application


framework written in PHP that adopts the
model-view-controller approach to
development

Why use CodeIgniter?

Feature rich
Lightweight/Ringan
Open source
Well-supported by an active community
Excellent by example documentation
Easy to configure
Supports multiple databases

Why use CodeIgniter?

In short, CodeIgniter is nice because it does


what it needs to do and then gets out of the
way.

Model-View-Controller

Model representation of the data


View rendering of the data suitable for
interaction with the user
Controller the traffic cop that passes model
data to the views and vice versa
This separation of concerns allows for greater
flexibility, reuse of code, and overall
preservation of the developers sanity

Controller

A class containing one or more related methods


(custom PHP functions)
Typical uses:

Request a set of data from the model by sending


arguments
Send a payload of data to a view (web page)
Receive a data payload from a view
Apply business logic to make decisions
Pass data to the model for inclusion in a database

View

Code that displays information to the user


Views can be:

Web pages with PHP code snippets inserted


Web pages with forms to gather user input
Other output (CSV, PDF, etc.)

Model

A class containing one or more related


methods (custom PHP functions)
Typical uses:

Create
Read
Update
Delete

CodeIgniter Classes

CIs built-in classes contain the basic


functionality that are frequently used by web
applications
The most-used classes are:

Database
Input
Loader
URI
Validation

Database Class

Generates queries using the Active Record


Pattern
Automatic escaping of input values
Provides method chaining for easy query
building
$this->db->where(name,$name);

Input Class

Pre-processes user input (prevents common


cross-site scripting techniques)
Provides access to user input and other data:

Form fields (POST)


Cookies
Server variables

$this->input->post(fieldname);

Loader Class

Makes various resources available:

Databases
Views
Helpers
Plugins

$this->load->view(viewname);

URI Class

Provides access to specific parts of the URI


string
Useful for building RESTful URIs
$this->uri->segment(n);

Validation Class

Helps validate user form input

Required fields
Required string formatting (length, regexp)

Enables success and failure messages on


form submittal
Enables re-population of form fields after form
submittal

Other Classes

Benchmarking
Calendaring
Email
Encryption
File uploading
FTP
HTML Table
Image Manipulation

Language
(internationalization)
Output
Pagination
Session
Trackback
Unit testing
XML-RPC
Zip encoding

Helpers and Plugins

CodeIgniter comes with a wide array of


helper functions that add convenience to
applications and provide ease of reuse.
$this->load->helper(helper_name);
CodeIgniter also allows for the use of custom
add-on functions called plugins.
$this->load->plugin(plugin_name);

My First CI Application
1.
2.
3.
4.
5.
6.

Unzip CI zip file into application folder on web


server
[optional] For pretty URLs, add .htaccess file
and enable mod_rewrite in Apache
Configure database in CIs <config.php>
Write controller (or modify existing)
Write view (or modify existing)
Write model

Demo