You are on page 1of 36

Introduction to CodeIgniter

Ed Finkler • http://funkatron.com • @funkatron


codeworks09
Thee Agenda

Why CodeIgniter?
The CI MVC Model
CI Basics
Running CI out of the box
Making a basic web app
Making a web api
http://www.flickr.com/photos/alternatewords/2332580309/

Why CodeIgniter?
Why not CakePHP or Zend
Framework or Limonade or
Symfony or Solar or Kohana
or Zoop or Yii or Akelos or
PHP on Trax or Prado or
Seagull?
Because you've gotta
pick one, dammit
All of them have value
That being said, CI is…
Easy to understand
Simple
doesn't require advanced OOP
Doesn't force lots of conventions
Plays well with others
Quick to get up and running
Good docs and great community
Backed by invested entity (http://ellislab.com)
CodeIgniter MVC
Implementation
More like
"Passive View Pattern"
Controller

View Model

http://short.ie/5o7eg4
CI application flow

Stolen from CI user guide


App components
Front controller Helper
Routing Plugin
Security Scripts
Controller View
Model Caching
Library
Front controller

index.php
Routing
http://domain.com/index.php/controller/method/param

class Search extends Controller


{

public function single($id)


{
// [...]
}
}
Security

Filtering or blocking unsafe input


Controller
The core of everything <?php
class Site extends Controller {

"Heavy": you could do function Site() {


parent::Controller();
everything in controller $this->load->library('session');
}
public methods are function index() {
available as actions from // mletters model is auto-loaded
$rows = $this->mletters->getMany(10);
URL $data['rows'] = $this->_prepData($rows);
$this->load->view('index', $data);
}
private methods prefixed
with “_” function _prepData($rows) {
// do some cleanup on the data…
}
?>
Model
ActiveRecord pattern available, not required
Query binding

$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3, 'live', 'Rick'));

Don't like the DB layer? Use something else


Zend_DB, Doctrine, DataMapper (http://bit.ly/
datamapper), IgniteRecord (http://bit.ly/igrec) …
Library

A class designed to work on related tasks


Helper
Procedural funcs, grouped by file
Mostly for views; available in controllers

/**
* Plural
*
* Takes a singular word and makes it plural
*
* @access public
* @param string
* @param bool
* @return str
*/
function plural($str, $force = FALSE)
{
// [...]
}
Plugin
Single procedural function
More extensive functionality than helper

$vals = array(
'word' => 'Random word',
'img_path' => './captcha/',
'img_url' => 'http://example.com/captcha/',
'font_path' => './system/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 7200
);

$cap = create_captcha($vals);
echo $cap['image'];
Script

Other scripts the CI app might use


View
Build response to client
CI Views are limited
Uses plain PHP as templating lang
<?php foreach ($rows as $row): ?>
<li class="letter">
<div class="body">
<h3>Dear Zend,</h3>
<p><?=$row->body?></p>
</div>
<div class="meta">
<div class="posted">
<a href="<?=site_url('/site/single/'.$row->id)?>">Posted <?=$row->posted?></a>
</div>
<div class="favorite">Liked by <?=$row->favorite_count?> person(s).
<a href="<?=site_url('/site/favorite/'.$row->id)?>">I like this</a>
</div>
</div>
</li>
<?php endforeach ?>
View
Optional template markup
$this->load->library('parser'); <html>
$this->parser->parse('blog_template', $data); <head>
<title>{blog_title}</title>
</head>
<body>

<h3>{blog_heading}</h3>

{blog_entries}
<h5>{title}</h5>
<p>{body}</p>
Want a heavier template lang? {/blog_entries}
Use one. </body>
</html>
Caching

Saves response to file


Serves up file contents if cache not expired
CI Basics

http://www.flickr.com/photos/canoafurada/395304306/
CI File Layout
front controller
index.php points to system and
application folders

system application

base classes & app-specific classes


built-in functionality & functionality
CI File Layout
default layout
CI File Layout
custom layout
only index.php is under
document root
The CI Object

$this inside controllers


The loader

$this->load->{view|library|model|helper|etc}('name');
CI Out of the box
The Welcome App

Put CI on the server


Load it in the browser
Why does Welcome load?
How URLs map
Trace with Xdebug/MacGDBp
Making a web application
Population estimates DB

Get our data from Numbrary: http://short.ie/w3f6h3


Make a new controller
Change the default route
Config DB settings
Make a model
Make a view
Make fancier views
Make a web API
http://www.flickr.com/photos/dunechaser/2429621774/
Web API for pop. est. DB

Let users query our DB via HTTP


Return results on JSON or serialized PHP
Questions?
http://www.flickr.com/photos/deadhorse/508559841/

@funkatron/#cw09
coj@funkatron.com

You might also like