Slide 1

Slide 1 text

Introduction to Introduction to CodeIgniter Zeroplex 2012/06/14

Slide 2

Slide 2 text

初學 PHP 買書 線上文件 2

Slide 3

Slide 3 text

B U T 3

Slide 4

Slide 4 text

書上不會提到 4

Slide 5

Slide 5 text

重複的程式碼 $user = trim( mysql_escape_string($_POST[‘user’] ) mysql_escape_string($_POST[‘user’] ) ); $pwd = trim( mysql_escape_string($_POST[‘pwd’] ) mysql_escape_string($_POST[‘pwd’] ) ); 5

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

架構 $ tree ● ● ├── html ├── lib ├── upload ├── config.php.sample ├── ├── global.php └── index.php 7

Slide 8

Slide 8 text

混亂的程式碼 8

Slide 9

Slide 9 text

混亂的程式碼 HTML 9

Slide 10

Slide 10 text

混亂的程式碼 CSS 10

Slide 11

Slide 11 text

混亂的程式碼 HTML 11

Slide 12

Slide 12 text

混亂的程式碼 HTML 12

Slide 13

Slide 13 text

混亂的程式碼 PHP 13

Slide 14

Slide 14 text

混亂的程式碼 Smarty Template Engine 14

Slide 15

Slide 15 text

問題仍然存在 相當耗時 不夠安全 不容易重複使用 佈署困難 15

Slide 16

Slide 16 text

PHP Frameworks Modules Templates Templates MVC DB Objects Validation Validation 16

Slide 17

Slide 17 text

PHP Frameworks 17

Slide 18

Slide 18 text

PHP Frameworks 較知名的 framework Zend CakePHP Symfony 18

Slide 19

Slide 19 text

Benchmarks ab –c 100 –n 30000 19

Slide 20

Slide 20 text

拿不定主意 20

Slide 21

Slide 21 text

直到 直到 PHPconf 2011 21

Slide 22

Slide 22 text

22

Slide 23

Slide 23 text

23

Slide 24

Slide 24 text

24

Slide 25

Slide 25 text

So .... 25

Slide 26

Slide 26 text

CodeIgniter CodeIgniter

Slide 27

Slide 27 text

CodeIgnter 麻雀雖小五藏俱全 27

Slide 28

Slide 28 text

CodeIgnter 麻雀雖小五藏俱全 沒有複雜的設定 28

Slide 29

Slide 29 text

CodeIgnter 麻雀雖小五藏俱全 沒有複雜的設定 效能佳 29

Slide 30

Slide 30 text

CodeIgnter 麻雀雖小五藏俱全 沒有複雜的設定 效能佳 中文文件 30

Slide 31

Slide 31 text

CodeIgnter 麻雀雖小五藏俱全 沒有複雜的設定 效能佳 中文文件 (其實只有一半是中文 XD) 31

Slide 32

Slide 32 text

CodeIgniter 簡介 Installation Structure Structure Configuration URLs Controller / Model / View Controller / Model / View Built-in Functions Sparks 32

Slide 33

Slide 33 text

Installation 1. wget 1. wget 2. unzip CodeIgniter-2.1.0.zip ..... Done ! 33

Slide 34

Slide 34 text

34

Slide 35

Slide 35 text

如果沒有成功 35

Slide 36

Slide 36 text

不要讓日落靠近 36

Slide 37

Slide 37 text

Structure ─── application ├── config ├── config ├── controllers ├── models ├── views ├── ........ ├── ........ ├── system └── index.php 37

Slide 38

Slide 38 text

Structure ─── application 網站程式所在位置 ├── config ├── config ├── controllers ├── models ├── views ├── ........ ├── ........ ├── system └── index.php 38

Slide 39

Slide 39 text

Structure ─── application ├── config ├── config ├── controllers ├── models ├── views ├── ........ ├── ........ ├── system CodeIgniter 核心 └── index.php 39

Slide 40

Slide 40 text

Configuration application/config config.php database.php autoload.php 40

Slide 41

Slide 41 text

Configuration application/config site URL config.php ─── database.php autoload.php site URL charset log/cache path session / cookie 41

Slide 42

Slide 42 text

Configuration application/config config.php database.php autoload.php 42

Slide 43

Slide 43 text

Configuration application/config config.php database.php autoload.php ─── 在程式執行時自動 載入模組或函式 43

Slide 44

Slide 44 text

URLs URL segment map to Controller index.php/post/show/2385 44

Slide 45

Slide 45 text

URLs URL segment map to Controller index.php/post/show/2385 Controller Argument 45 Argument Method

Slide 46

Slide 46 text

URLs class Post extends CI_Controller { class Post extends CI_Controller { public function show($id){ // Get post information } } } 46

Slide 47

Slide 47 text

Controller application/controller/welcome.php: class Welcome extends CI_Controller { public function index(){ $this->load-> view('welcome_message'); } } 47

Slide 48

Slide 48 text

Controller application/controller/welcome.php: class Welcome extends CI_Controller { public function index(){ $this->load-> view('welcome_message'); } } 48

Slide 49

Slide 49 text

Controller application/controller/welcome.php: class Welcome extends CI_Controller { public function index(){ $this->load-> view('welcome_message'); } } 49

Slide 50

Slide 50 text

Your Own Controller controller/hello.php class Hello extends CI_Controller { class Hello extends CI_Controller { public function greeting($id){ echo “Hello $id”; } } } 50

Slide 51

Slide 51 text

Your Own Controller controller/hello.php class Hello extends CI_Controller { class Hello extends CI_Controller { public function greeting($id){ echo “Hello $id”; } } } Print ‘Hello C4Labs’ : index.php/hello/greeting/C4Labs 51

Slide 52

Slide 52 text

Your Own Controller Deny method from URL access class Hello extends CI_Controller { public function _greeting($id){ echo “Hello $id”; echo “Hello $id”; } } 52 Underline

Slide 53

Slide 53 text

View 位於 application/view/ 由 controller 載入 Template parser 53

Slide 54

Slide 54 text

View application/view/hello.php

Hello Hello

54

Slide 55

Slide 55 text

View Load view function hello($id){ $data[‘id’] = $id; $this->load->view(‘hello’, $data); $this->load->view(‘hello’, $data); } 55

Slide 56

Slide 56 text

Template Parser

Hello

PHP

Slide 57

Slide 57 text

Template Parser

Hello {id}

Slide 58

Slide 58 text

Template Parser function hello($id){ $this->load->library(‘parser’); $this->load->library(‘parser’); $data[‘id’] = $id; $this->parser-> parse(‘hello', $data); parse(‘hello', $data); } 58

Slide 59

Slide 59 text

Model Process data in database class User extends CI_Model{ function getUser($uid) { ... } function deleteUser($uid) { ... } function deleteUser($uid) { ... } } 59

Slide 60

Slide 60 text

Model Load model in controller $this->load->model(‘user’); $user = $this->user->getUser(2); 60

Slide 61

Slide 61 text

Built-in Functions Library Input Template Parser File Uploading Helper URL URL CAPTCHA 61

Slide 62

Slide 62 text

Built-in Functions Load $this->load->library(‘upload’); $this->load->helper(‘url’); Use $this->upload->data(); $this->upload->data(); echo site_url(); 62

Slide 63

Slide 63 text

No enough? 63

Slide 64

Slide 64 text

Sparks ! 64

Slide 65

Slide 65 text

Sparks Package management system for CodeIgniter CodeIgniter Easy to install Lots of useful packages Makes you lazy 65

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

Using Sparks $ php tools/spark search redis menu - The Menu library is used to .... 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

Slide 68

Slide 68 text

Using Packages Load and call $this->load->spark(‘redis/0.3.0’); $this->redis->set(‘foo’, ‘bar’); $this->redis->set(‘foo’, ‘bar’); 68

Slide 69

Slide 69 text

More CodeIgniter 中文討論區 http://www.codeigniter.org.tw/forum/ http://www.codeigniter.org.tw/forum/ CodeIgniter Wiki http://codeigniter.com/wiki 69

Slide 70

Slide 70 text

Q & A 70

Slide 71

Slide 71 text

Live Debug Demo 71

Slide 72

Slide 72 text

Thanks You 72