Introduction to CodeIgniter Ed Finkler http://funkatron.com - - PowerPoint PPT Presentation

introduction to codeigniter
SMART_READER_LITE
LIVE PREVIEW

Introduction to CodeIgniter Ed Finkler http://funkatron.com - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Introduction to CodeIgniter

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

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Why CodeIgniter?

http://www.flickr.com/photos/alternatewords/2332580309/

slide-4
SLIDE 4

Why not CakePHP or Zend Framework or Limonade or Symfony or Solar or Kohana

  • r Zoop or Yii or Akelos or

PHP on Trax or Prado or Seagull?

slide-5
SLIDE 5

Because you've gotta pick one, dammit

slide-6
SLIDE 6

All of them have value

slide-7
SLIDE 7

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)

slide-8
SLIDE 8

CodeIgniter MVC Implementation

slide-9
SLIDE 9

More like "Passive View Pattern"

http://short.ie/5o7eg4 Controller Model View

slide-10
SLIDE 10

CI application flow

Stolen from CI user guide

slide-11
SLIDE 11

App components

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

slide-12
SLIDE 12

Front controller

index.php

slide-13
SLIDE 13

Routing

class Search extends Controller { public function single($id) { // [...] } } http://domain.com/index.php/controller/method/param

slide-14
SLIDE 14

Security

Filtering or blocking unsafe input

slide-15
SLIDE 15

Controller

The core of everything "Heavy": you could do everything in controller public methods are available as actions from URL private methods prefixed with “_”

<?php class Site extends Controller { function Site() { parent::Controller(); $this->load->library('session'); } function index() { // mletters model is auto-loaded $rows = $this->mletters->getMany(10); $data['rows'] = $this->_prepData($rows); $this->load->view('index', $data); } function _prepData($rows) { // do some cleanup on the data… } ?>

slide-16
SLIDE 16

Model

ActiveRecord pattern available, not required Query binding Don't like the DB layer? Use something else Zend_DB, Doctrine, DataMapper (http://bit.ly/ datamapper), IgniteRecord (http://bit.ly/igrec) …

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

slide-17
SLIDE 17

Library

A class designed to work on related tasks

slide-18
SLIDE 18

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) { // [...] }

slide-19
SLIDE 19

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'];

slide-20
SLIDE 20

Script

Other scripts the CI app might use

slide-21
SLIDE 21

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 ?>

slide-22
SLIDE 22

View

Optional template markup Want a heavier template lang? Use one.

<html> <head> <title>{blog_title}</title> </head> <body> <h3>{blog_heading}</h3> {blog_entries} <h5>{title}</h5> <p>{body}</p> {/blog_entries} </body> </html> $this->load->library('parser'); $this->parser->parse('blog_template', $data);

slide-23
SLIDE 23

Caching

Saves response to file Serves up file contents if cache not expired

slide-24
SLIDE 24

CI Basics

http://www.flickr.com/photos/canoafurada/395304306/

slide-25
SLIDE 25

CI File Layout

index.php system application

base classes & built-in functionality app-specific classes & functionality front controller points to system and application folders

slide-26
SLIDE 26

CI File Layout

default layout

slide-27
SLIDE 27

CI File Layout

custom layout

  • nly index.php is under

document root

slide-28
SLIDE 28

The CI Object

$this inside controllers

slide-29
SLIDE 29

The loader

$this->load->{view|library|model|helper|etc}('name');

slide-30
SLIDE 30

CI Out of the box

slide-31
SLIDE 31

The Welcome App

Put CI on the server Load it in the browser Why does Welcome load? How URLs map Trace with Xdebug/MacGDBp

slide-32
SLIDE 32

Making a web application

slide-33
SLIDE 33

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

slide-34
SLIDE 34

Make a web API

http://www.flickr.com/photos/dunechaser/2429621774/

slide-35
SLIDE 35

Web API for pop. est. DB

Let users query our DB via HTTP Return results on JSON or serialized PHP

slide-36
SLIDE 36

Questions?

@funkatron/#cw09 coj@funkatron.com

http://www.flickr.com/photos/deadhorse/508559841/