introduction to codeigniter

72
Introduction to CodeIgniter Zeroplex 2012/06/14

Upload: zero-huang

Post on 05-Jul-2015

2.142 views

Category:

Technology


2 download

DESCRIPTION

Small Talk @ C4Labs

TRANSCRIPT

Page 1: Introduction to Codeigniter

Introduction to

CodeIgniter

Zeroplex

2012/06/14

Page 2: Introduction to Codeigniter

初學 PHP

買書

線上文件

2

Page 3: Introduction to Codeigniter

3

B U T

Page 4: Introduction to Codeigniter

4

書上不會提到

Page 5: Introduction to Codeigniter

重複的程式碼

$user = trim(

mysql_escape_string($_POST[„user‟] )

);

$pwd = trim(

mysql_escape_string($_POST[„pwd‟] )

);

5

Page 6: Introduction to Codeigniter

6

參考 PHPWind 程式碼,後重新修改自用程式架構

Page 7: Introduction to Codeigniter

架構

$ tree●

├── html

├── lib

├── upload

├── config.php.sample

├── global.php

└── index.php

7

Page 8: Introduction to Codeigniter

8

混亂的程式碼

Page 9: Introduction to Codeigniter

9

混亂的程式碼

HTML

Page 10: Introduction to Codeigniter

10

混亂的程式碼

CSS

Page 11: Introduction to Codeigniter

11

混亂的程式碼

HTML

Page 12: Introduction to Codeigniter

12

混亂的程式碼

HTML

Page 13: Introduction to Codeigniter

13

混亂的程式碼

PHP

Page 14: Introduction to Codeigniter

混亂的程式碼

Smarty Template Engine

14

Page 15: Introduction to Codeigniter

問題仍然存在

相當耗時

不夠安全

不容易重複使用

佈署困難

15

Page 16: Introduction to Codeigniter

PHP Frameworks

Modules

Templates

MVC

DB Objects

Validation

16

Page 17: Introduction to Codeigniter

PHP Frameworks

17

Page 18: Introduction to Codeigniter

PHP Frameworks

較知名的 framework

Zend

CakePHP

Symfony

18

Page 19: Introduction to Codeigniter

Benchmarks

ab –c 100 –n 30000

19

Page 20: Introduction to Codeigniter

20

拿不定主意

Page 21: Introduction to Codeigniter

21

直到

PHPconf 2011

Page 22: Introduction to Codeigniter

22

Page 23: Introduction to Codeigniter

23

Page 24: Introduction to Codeigniter

24

Page 25: Introduction to Codeigniter

25

So ....

Page 26: Introduction to Codeigniter

CodeIgniter

Page 27: Introduction to Codeigniter

CodeIgnter

麻雀雖小五藏俱全

27

Page 28: Introduction to Codeigniter

CodeIgnter

麻雀雖小五藏俱全

沒有複雜的設定

28

Page 29: Introduction to Codeigniter

CodeIgnter

麻雀雖小五藏俱全

沒有複雜的設定

效能佳

29

Page 30: Introduction to Codeigniter

CodeIgnter

麻雀雖小五藏俱全

沒有複雜的設定

效能佳

中文文件

30

Page 31: Introduction to Codeigniter

CodeIgnter

麻雀雖小五藏俱全

沒有複雜的設定

效能佳

中文文件 (其實只有一半是中文 XD)

31

Page 32: Introduction to Codeigniter

CodeIgniter 簡介

Installation

Structure

Configuration

URLs

Controller / Model / View

Built-in Functions

Sparks

32

Page 33: Introduction to Codeigniter

Installation

1. wget

2. unzip CodeIgniter-2.1.0.zip

..... Done !

33

Page 34: Introduction to Codeigniter

34

Page 35: Introduction to Codeigniter

35

如果沒有成功

Page 36: Introduction to Codeigniter

36

不要讓日落靠近

Page 37: Introduction to Codeigniter

Structure

─── application

├── config

├── controllers

├── models

├── views

├── ........

├── system

└── index.php

37

Page 38: Introduction to Codeigniter

Structure

─── application 網站程式所在位置

├── config

├── controllers

├── models

├── views

├── ........

├── system

└── index.php

38

Page 39: Introduction to Codeigniter

Structure

─── application

├── config

├── controllers

├── models

├── views

├── ........

├── system CodeIgniter 核心

└── index.php

39

Page 40: Introduction to Codeigniter

Configuration

application/config

config.php

database.php

autoload.php

40

Page 41: Introduction to Codeigniter

Configuration

application/config

config.php ───

database.php

autoload.php

41

site URLcharsetlog/cache pathsession / cookie

Page 42: Introduction to Codeigniter

Configuration

application/config

config.php

database.php

autoload.php

42

Page 43: Introduction to Codeigniter

Configuration

application/config

config.php

database.php

autoload.php ───

43

在程式執行時自動載入模組或函式

Page 44: Introduction to Codeigniter

URLs

URL segment map to Controller

index.php/post/show/2385

44

Page 45: Introduction to Codeigniter

URLs

URL segment map to Controller

index.php/post/show/2385

45

ControllerArgument

Method

Page 46: Introduction to Codeigniter

URLs

class Post extends CI_Controller {

public function show($id){

// Get post information

}

}

46

Page 47: Introduction to Codeigniter

Controller

application/controller/welcome.php:

class Welcome extends CI_Controller {

public function index(){

$this->load->

view('welcome_message');

}

}

47

Page 48: Introduction to Codeigniter

Controller

application/controller/welcome.php:

class Welcome extends CI_Controller {

public function index(){

$this->load->

view('welcome_message');

}

}

48

Page 49: Introduction to Codeigniter

Controller

application/controller/welcome.php:

class Welcome extends CI_Controller {

public function index(){

$this->load->

view('welcome_message');

}

}

49

Page 50: Introduction to Codeigniter

Your Own Controller

controller/hello.php

class Hello extends CI_Controller {

public function greeting($id){

echo “Hello $id”;

}

}

50

Page 51: Introduction to Codeigniter

Your Own Controller

controller/hello.php

class Hello extends CI_Controller {

public function greeting($id){

echo “Hello $id”;

}

}

Print „Hello C4Labs‟ :

index.php/hello/greeting/C4Labs51

Page 52: Introduction to Codeigniter

Your Own Controller

Deny method from URL access

class Hello extends CI_Controller {

public function _greeting($id){

echo “Hello $id”;

}

}

52

Underline

Page 53: Introduction to Codeigniter

View

位於 application/view/

由 controller 載入

Template parser

53

Page 54: Introduction to Codeigniter

View

application/view/hello.php

<html><body>

<h1>

Hello <?php echo $id;?>

</h1>

</html></body>

54

Page 55: Introduction to Codeigniter

View

Load view

function hello($id){

$data[„id‟] = $id;

$this->load->view(„hello‟, $data);

}

55

Page 56: Introduction to Codeigniter

Template Parser

<html><body>

<h1>

Hello <?php echo $id;?>

</h1>

</html></body>

56

PHP

Page 57: Introduction to Codeigniter

Template Parser

<html><body>

<h1>

Hello {id}

</h1>

</html></body>

57

Page 58: Introduction to Codeigniter

Template Parser

function hello($id){

$this->load->library(„parser‟);

$data[„id‟] = $id;

$this->parser->

parse(„hello', $data);

}

58

Page 59: Introduction to Codeigniter

Model

Process data in database

class User extends CI_Model{

function getUser($uid) { ... }

function deleteUser($uid) { ... }

}

59

Page 60: Introduction to Codeigniter

Model

Load model in controller

$this->load->model(„user‟);

$user = $this->user->getUser(2);

60

Page 61: Introduction to Codeigniter

Built-in Functions

Library

Input

Template Parser

File Uploading

Helper

URL

CAPTCHA

61

Page 62: Introduction to Codeigniter

Built-in Functions

Load

$this->load->library(„upload‟);

$this->load->helper(„url‟);

Use

$this->upload->data();

echo site_url();

62

Page 63: Introduction to Codeigniter

63

No enough?

Page 64: Introduction to Codeigniter

64

Sparks !

Page 65: Introduction to Codeigniter

Sparks

Package management system for CodeIgniter

Easy to install

Lots of useful packages

Makes you lazy

65

Page 66: Introduction to Codeigniter

Install Sparks

66

php -r "$(curl -fsSL http://getsparks.org/go-sparks)"

Page 67: Introduction to Codeigniter

Using Sparks

$ php tools/spark search redis

menu - The Menu library is used to ....

redis - A CodeIgniter library to ....

$ php tools/spark install redis

Retrieving spark detail from getsparks.org

........

Spark installed to ./sparks/redis/0.3.0 - You're on fire!

67

Page 68: Introduction to Codeigniter

Using Packages

Load and call

$this->load->spark(„redis/0.3.0‟);

$this->redis->set(„foo‟, „bar‟);

68

Page 69: Introduction to Codeigniter

More

CodeIgniter 中文討論區http://www.codeigniter.org.tw/forum/

CodeIgniter Wikihttp://codeigniter.com/wiki

69

Page 70: Introduction to Codeigniter

70

Q & A

Page 71: Introduction to Codeigniter

71

Live Debug

Demo

Page 72: Introduction to Codeigniter

72

Thank You