Phpconf 2011 introduction_to_codeigniter

65
快速上手 快速上手 CodeIgniter CodeIgniter 吳柏毅 Bo-Yi Wu appleboy http://blog.wu-boy.com/ 2011.11.12 2011 PHP Conference

description

http://phpconf.tw/2011 快速上手 CodeIgniter 吳柏毅 (Bo-Yi Wu)/appleboy Introduction to CodeIgniter

Transcript of Phpconf 2011 introduction_to_codeigniter

Page 1: Phpconf 2011 introduction_to_codeigniter

快速上手 快速上手 CodeIgniterCodeIgniter

吳柏毅 Bo-Yi Wuappleboy

http://blog.wu-boy.com/2011.11.12

2011 PHP Conference

Page 2: Phpconf 2011 introduction_to_codeigniter

內容皆採用 創用 CC 姓名標示 相同方式分享 3.0 台灣 授權條款

Page 3: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 3

$this->load->view('about/me')

瑞昱半導體股份有限公司 Linux Kernel Driver, Sencha Touch Web App.

CodeIgniter 台灣站長 http://codeigniter.org.tw 翻譯線上文件

提交 Patch給官方 CodeIgniter

個人 Github https://github.com/appleboy

Page 4: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 4

本投影片適合尚未使用過Framework且對於 PHP已經有些

基礎的Web Developer

Page 5: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 5

Why use Framework?

合作方式 (不管你是團隊還是 Soho) 程式設計師與程式設計師

程式設計師與前端設計師

加速開發 幫你寫好一堆功能模組 (分頁 ,驗證碼 ,多國語系 ) 幫你解決安全性問題 ($_GET, $POST ...)

Page 6: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 6

在 2009年 1月畢業進入資策會的第 1份工作

CodeIgniter與我

Page 7: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 7

為了尋找一套適合同事們一起共同開發的環境架構

(Zend, Cake, Symfony, Yii...)

Page 8: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 8

$this->load->view('Why')

在眾多 Framework選擇下為什麼要使用 CodeIgniter

Page 9: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 9

初學者非用 CodeIgniter 的優勢

懶人安裝方法

架構清楚明瞭

繁體中文文件

基礎MVC架構 (日後可跳往其它 Framework)

Page 10: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 10

懶人安裝方法

Page 11: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 11

下載 + 解壓縮 = 安裝完成適用於任何作業系統

Page 12: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 12

真的這麼容易安裝 ?

進入下載網址 http://www.codeigniter.org.tw/downloads

解壓縮檔案 unzip CodeIgniter_2.X.X.zip

輸入網址 http://your_host_name/codeigniter/

Page 13: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 13

$this->load->view('welcome')

Page 14: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 14

如果安裝失敗呢?

Page 15: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 15

你還是不要接觸 PHP會比較好(開玩笑的 )

Page 16: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 16

CodeIgniter架構清楚明瞭

Page 17: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 17

CodeIgniter 目錄

application (網站主目錄 )

system (CodeIgniter 核心目錄 )

user_guide (CodeIgniter 使用手冊 )

index.php (網站主程式 )

Page 18: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 18

開發多網站目錄架構(大部份 Framework做法 )

Page 19: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 19

多網站目錄架構

system (升級版本只需要換此目錄 )

web_01

index.php

application

web_01

index.php

application

web_02

index.php

application

Page 20: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 20

簡易清楚繁體中文文件(解決初學者對於英文的排斥 )

http://codeigniter.org.tw/user_guide/

Page 21: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 21

初學者請直接閱讀一般主題

Page 22: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 22

必讀資料

一般主題 CodeIgniter URLs 控制器 (Controllers) 檢視 (Views) 模型 (Models)

類別參考 Database 類別 Input 類別 Loader 類別

Page 23: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 23

進階閱讀

一般主題● 新增程式庫 (Library)● 新增核心類別

● Hooks – 擴充核心● URI 路由

類別參考● Email 類別● File Uploading 類別● Form Validation 類別● Language 類別● Output 類別● Pagination 類別● Session 類別

Page 24: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 24

以上就是足以讓大家嘗試的理由

簡單 容易 好上手

Page 25: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 25

來看看 CodeIgniter基本MVC架構

Page 26: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 26

Web Server

Routing

Controller

ViewModel

LayoutDatabase

Client Browser

MVC Architecture

Library Helper

Caching

Page 27: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 27

撰寫第一個 Hello World

Page 28: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 28

View

Controller

View (views/welcome.php)<html> <body> Hello <?php echo $username; ?> </body></html>

Controller (controllers/welcome.php) function welcome($id) { $data[‘username’] = $id; $this->load->view(“welcome”, $data);

}

Hello Appleboy

Page 29: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 29

CodeIgniter 功能特性介紹

Page 30: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 30

自訂 URL

還在用 Apache模組mod_rewrite自訂特殊 URL嘛?

Page 31: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 31

CodeIgniter內建自訂 URL功能

$route['products/([a-z]+)/(\d+)'] = "$1/id_$2" products/os/1234 呼叫 os controller 傳入參數 id_1234

網站出現重大問題需要停機修復 $route['(:any)'] = "system/fix" $route['.*'] = “system/fix”

Page 32: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 32

High Performance Framework

CodeIgniter預設不會載入未使用模組用到時自行載入

Page 33: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 33

High Performance Framework

載入 Library $this->load->library(array('email', 'table'));

載入 view $this->load->view('file_name');

載入model $this->load->model('model_name');

載入 helper $this->load->helper('help_name');

Page 34: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 34

CodeIgniter Cahe System

index

Caching

Routing

Application Controller

Cache 存在

Page 35: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 35

Cache 使用方式

Cache 可以寫在 Controller 任意地方 $this->output->cache(n); n代表分鐘

Page 36: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 36

Cache 缺點

當 expire time尚未過期

網頁資訊一定是舊的

Page 37: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 37

CodeIgniter Cache 機制

將 time()+n*60 寫入到 cache檔案最前面

每次 Request 則取出跟 time() 比對時間 若 cache time > time()直接輸出 cache file 若 cache time < time()刪除 cache並且新增 cache

缺點 : 適用於靜態檔案

Page 38: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 38

如果用在動態檔案呢 ?

官方無提供刪除 cache檔案函式請自行 Patch程式碼

http://goo.gl/03IrZ$this->output->delete_cache('post/12')$this->output->delete_cache('post/list')

Page 39: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 39

支援簡單 Command Line

CodeIgniter 2.x.x 版本支援Windows, Linux crontab 搭配背景執行處理

Page 40: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 40

好處

CLI去執行您的 cron-jobs而不需要使用 curl

互動式 "tasks" 工作 , 像是動態改變權限、清除cache …目錄、執行備份 等。

任意搭配其他程式 ,例如 Perl, C++, Python

Page 41: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 41

用法

$ php index.php controller method$ php index.php welcome index

Page 42: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 42

避免網頁執行到 command line

將 corn-jobs 移出 apache可執行目錄

用 PHP判斷,確保瀏覽器不能執行 cron-jobs php_sapi_name() === "cli" defined('STDIN')

Page 43: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 43

處理 $_POST,$_GET資料

if ( ! isset($_POST['user']))

$user = FALSE;

else

$user = $_POST['user'];

$user = (! isset($_POST['user'])) ? FALSE : $_POST['user'];

Page 44: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 44

不需要這麼麻煩

$_POST資料 $user = $this->input->post('user');

$_GET資料 $user = $this->input->get('user');

一起判斷 $user = $this->input->get_post('user'); 先找 $_POST['user'], 後找 $_GET['user']

Page 45: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 45

避免 XSS 的攻擊

$user = $this->input->post('user', TRUE); $user = $this->input->get('user', TRUE); $user = $this->input->get_post('user', TRUE);

Page 46: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 46

良好的程式設計

善用 === 做判斷

數字請用 intval()或 (int)過慮 $id = intavl($id); $id = (int) $id;

用 is_array, is_int, is_bool, is_string判斷類型

Page 47: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 47

表單驗證

$this->form_validation->set_rules('username', '帳號 ', 'trim|required|min_length[5]|max_length[12]|xss_clean');

$this->form_validation->set_rules('password', '密碼 ', 'trim|required|md5');

$this->form_validation->set_rules('email', '電子郵件 ', 'trim|required|valid_email');

Page 48: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 48

分頁

$this->load->library('pagination'); $config['base_url'] =

'http://example.com/index.php/test/page/'; $config['total_rows'] = 200; $config['per_page'] = 20; $this->pagination->initialize($config); echo $this->pagination->create_links();

Page 49: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 49

Email 寄信

多重協定 : Mail, Sendmail, and SMTP

多重收件人

副本 (CC)和密件副本 (BCCs)

支援 HTML或者是純文字 (Plaintext)郵件

附件檔案

Page 50: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 50

使用方式

$this->load->library('email');

$this->email->from('[email protected]', 'Your Name');

$this->email->to('[email protected]');

$this->email->cc('[email protected]');

$this->email->bcc('[email protected]');

$this->email->subject('Email Test');

$this->email->message('Testing the email class.');

$this->email->send();

echo $this->email->print_debugger();

Page 51: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 51

影像縮圖功能

修改影像的尺寸

建立縮圖

影像裁剪

影像旋轉

浮水印

支援 GD/GD2, NetPBM以及 ImageMagick

Page 52: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 52

以上只是一些CodeIgniter Library基本介紹想了解更多功能請上

User Guidehttp://goo.gl/7PGnW

Page 53: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 53

光是 CodeIgniter內建的功能還不能滿足您嘛?

Page 54: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 54

歡迎使用 http://getsparks.org/

Page 55: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 55

Ruby有 RubyGemsNode.js 有 npm

CodeIgniter有 sparks

What is Sparks?

Page 56: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 56

What is Sparks?

Package Management System

Making Code Easy to Find, Create, and Distribute

Page 57: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 57

Get Sparks tool Now!!

一行指令就安裝完成$ php -r "$(curl -fsSL http://getsparks.org/go-sparks)"

Page 58: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 58

Installing Sparks with the Spark Manager

http://goo.gl/lHmCX$ php tools/spark install -v1.0.4 google-url-shortener

Page 59: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 59

Load Sparks Library

$this->load->spark(google-url-shortener/1.0.4');$short_url = $this->google_url_api->shorten($url);echo $url . " => " . $short_url->id . "<br />";

Page 60: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 60

好用 sparks 介紹

ion_auth http://goo.gl/Au4kM E-mail 啟動帳號 ,忘記密碼 ..等 單一帳號多重群組

User/Email雙重認證選擇 Zend ACL Library 導入權限 搭配 Facebook, Google Auth API 認證

Template http://goo.gl/BN5g6 動態讀取 CSS或 Javascript

Page 61: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 61

CodeIgniter 重大改變 2009->2011

版本演進 1.7.x → 2.x.x 捨棄 PHP4 效能改善

版本控制 Bitbucket → Github 加速大家貢獻

Page 62: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 62

如果有任何問題都可以到討論區留言http://www.codeigniter.org.tw/forum/

Page 63: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 63

歡迎加入 CodeIgniter翻譯

Page 64: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 64

https://github.com/appleboy/PHP-CodeIgniter-Framework-Taiwan

縮址 : http://goo.gl/5CU9N

Page 65: Phpconf 2011 introduction_to_codeigniter

2011/11/12 PHP Conference 65

Thank You.