introduction to codeigniter

Post on 05-Jul-2015

2.142 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Small Talk @ C4Labs

TRANSCRIPT

Introduction to

CodeIgniter

Zeroplex

2012/06/14

初學 PHP

買書

線上文件

2

3

B U T

4

書上不會提到

重複的程式碼

$user = trim(

mysql_escape_string($_POST[„user‟] )

);

$pwd = trim(

mysql_escape_string($_POST[„pwd‟] )

);

5

6

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

架構

$ tree●

├── html

├── lib

├── upload

├── config.php.sample

├── global.php

└── index.php

7

8

混亂的程式碼

9

混亂的程式碼

HTML

10

混亂的程式碼

CSS

11

混亂的程式碼

HTML

12

混亂的程式碼

HTML

13

混亂的程式碼

PHP

混亂的程式碼

Smarty Template Engine

14

問題仍然存在

相當耗時

不夠安全

不容易重複使用

佈署困難

15

PHP Frameworks

Modules

Templates

MVC

DB Objects

Validation

16

PHP Frameworks

17

PHP Frameworks

較知名的 framework

Zend

CakePHP

Symfony

18

Benchmarks

ab –c 100 –n 30000

19

20

拿不定主意

21

直到

PHPconf 2011

22

23

24

25

So ....

CodeIgniter

CodeIgnter

麻雀雖小五藏俱全

27

CodeIgnter

麻雀雖小五藏俱全

沒有複雜的設定

28

CodeIgnter

麻雀雖小五藏俱全

沒有複雜的設定

效能佳

29

CodeIgnter

麻雀雖小五藏俱全

沒有複雜的設定

效能佳

中文文件

30

CodeIgnter

麻雀雖小五藏俱全

沒有複雜的設定

效能佳

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

31

CodeIgniter 簡介

Installation

Structure

Configuration

URLs

Controller / Model / View

Built-in Functions

Sparks

32

Installation

1. wget

2. unzip CodeIgniter-2.1.0.zip

..... Done !

33

34

35

如果沒有成功

36

不要讓日落靠近

Structure

─── application

├── config

├── controllers

├── models

├── views

├── ........

├── system

└── index.php

37

Structure

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

├── config

├── controllers

├── models

├── views

├── ........

├── system

└── index.php

38

Structure

─── application

├── config

├── controllers

├── models

├── views

├── ........

├── system CodeIgniter 核心

└── index.php

39

Configuration

application/config

config.php

database.php

autoload.php

40

Configuration

application/config

config.php ───

database.php

autoload.php

41

site URLcharsetlog/cache pathsession / cookie

Configuration

application/config

config.php

database.php

autoload.php

42

Configuration

application/config

config.php

database.php

autoload.php ───

43

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

URLs

URL segment map to Controller

index.php/post/show/2385

44

URLs

URL segment map to Controller

index.php/post/show/2385

45

ControllerArgument

Method

URLs

class Post extends CI_Controller {

public function show($id){

// Get post information

}

}

46

Controller

application/controller/welcome.php:

class Welcome extends CI_Controller {

public function index(){

$this->load->

view('welcome_message');

}

}

47

Controller

application/controller/welcome.php:

class Welcome extends CI_Controller {

public function index(){

$this->load->

view('welcome_message');

}

}

48

Controller

application/controller/welcome.php:

class Welcome extends CI_Controller {

public function index(){

$this->load->

view('welcome_message');

}

}

49

Your Own Controller

controller/hello.php

class Hello extends CI_Controller {

public function greeting($id){

echo “Hello $id”;

}

}

50

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

Your Own Controller

Deny method from URL access

class Hello extends CI_Controller {

public function _greeting($id){

echo “Hello $id”;

}

}

52

Underline

View

位於 application/view/

由 controller 載入

Template parser

53

View

application/view/hello.php

<html><body>

<h1>

Hello <?php echo $id;?>

</h1>

</html></body>

54

View

Load view

function hello($id){

$data[„id‟] = $id;

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

}

55

Template Parser

<html><body>

<h1>

Hello <?php echo $id;?>

</h1>

</html></body>

56

PHP

Template Parser

<html><body>

<h1>

Hello {id}

</h1>

</html></body>

57

Template Parser

function hello($id){

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

$data[„id‟] = $id;

$this->parser->

parse(„hello', $data);

}

58

Model

Process data in database

class User extends CI_Model{

function getUser($uid) { ... }

function deleteUser($uid) { ... }

}

59

Model

Load model in controller

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

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

60

Built-in Functions

Library

Input

Template Parser

File Uploading

Helper

URL

CAPTCHA

61

Built-in Functions

Load

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

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

Use

$this->upload->data();

echo site_url();

62

63

No enough?

64

Sparks !

Sparks

Package management system for CodeIgniter

Easy to install

Lots of useful packages

Makes you lazy

65

Install Sparks

66

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

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

Using Packages

Load and call

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

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

68

More

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

CodeIgniter Wikihttp://codeigniter.com/wiki

69

70

Q & A

71

Live Debug

Demo

72

Thank You

top related