Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
11 - CodeIgniter - OpenWebSchool
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
openwebschool
August 22, 2012
Programming
390
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
11 - CodeIgniter - OpenWebSchool
openwebschool
August 22, 2012
Transcript
30 slides
Ensky / 林宏昱
What's Framework Framework ,中文是「框架」 顧名思義就是一個框框,你寫的東西都要在那個框 框裡。
What's Framework 更精確的定義,就是有人寫了一些 code , 那些 code 提供了一個規範, 規定你怎麼寫網站、檔案應該存在哪裡, 透過那些規定,你可能可以更快寫出一個架構不錯
的網站。
你可能會覺得, PHP 那麼簡單,我也寫很快阿, 為什麼還需要 Framework 來「規範」我呢? 我崇尚自由!
Why Framework? 但是阿,有句諺語說, 「 code 過了三個月就不是你寫的了」 自己寫的 code 要是沒有 document
, 很快就忘了你之前的邏輯
或者是,你在團隊中工作, 而他們全部都需要了解你的 Coding style 。 身為工程師的你超懶得寫 document 的阿
Why Framework? • 你需要大量 Document 讓你的工作伙伴快速上手 • 你需要一套規範讓大家 coding 邏輯相同
而這些是一個好的 Framework 會提供給你的!
Framework to NewHand 基本上不建議新手學 Framework 。 新手對於 PHP 的各種特性還不夠熟悉, 很容易被
Framework 帶來的各種「糖衣」誘惑 一旦使用之後從此對於架構的概念就定型了 從此在也無法接受其他架構 (即使別的 FrameWork 可能設計的更好)
Framework to NewHand Framework 也是人寫出來的, 在使用的時候你一定要知道每個功能背後是 怎麼寫的,甚至你可以自己 trace code 。
如此一來,才不會被 Framework 綁住, 輕則不能輕易換 Framework , 重則沒有 Framework 就不會寫 code 了。
Framework to NewHand 因此我對新手的建議是 多練、多嘗試 趁自己的架構還沒有被定型, 多嘗試研發自己的架構, 寫的順了,想快速開發,再 survey 一套順手的
Framework 而不是一開始就直接碰他。
Popular PHP Framework http://www.phpframeworks.com/index.php
CodeIgniter is an PHP Framework • 小巧 • 輕量 •
說明文件多、完整 • 速度快 • 簡單 我講 CI 不代表 CI 就是最好的 Framework , 每個人喜歡的 Framework 不同。
Model View Controller MVC 是一個程式設計的概念, 和 OOP 很像, 並不是網頁設計獨有的東西。 但在網頁設計上剛好很適合採用
M-V-C 架構, 因此幾乎大家的 PHP Framework 都採此架構設計。
Model View Controller Model View Controller
Model View Controller • Controller – 邏輯判斷 Ex: 這頁做些什麼事情?(登入、註冊、表單) 我需要哪些資料?
中間人的角色 • Model – 可以重複利用的函數群 可以被不同的 Controller load 比如說 Database 的操作很適合成為 Model
Model View Controller • View – Layout 各種不同的網頁內容 此種頁面純粹印出 Controller
提供的變數 ( or 陣列) 不做任何邏輯判斷。
Model View Controller Model View Controller Browser 1.GET /login 2.Load
Auth model 3.Return Auth model 4.Load Login page 5. Return Login Page 6. Return Page
Router CI 的檔案管理是用 URL PATH 管理的, 當一個網址長這樣: http://www.codeigniter.org.tw/downloads 他其實代表的意思是: http://www.codeigniter.org.tw/index.php/downloads
而利用 Web Server 的 URL Rewrite 功能, 達到重新導向的目的。 因此,所有網址都經由 index.php 做統一集中的 route 動作
Router index.php 看到一個 url 長這樣 http://ensky.tw/auth/login 根據 CI 的 router
,他會去找 controller/auth.php 這隻程式,然後去執行他的 login function Demo Code
Router 而如果再繼續 / 下去,則會被當成該 method 的 params 傳進去。 也因為 CI
的 Router 機制, 我們看到一個 url 就知道去哪裡找他, 檔案在哪裡都一清二楚不會找不到。 http://xxxx/controller/method/params
Loader 為了加快速度, CI 的所有元件都是 load by need 的, 需要的時候在使用 loader
把它 load 進來。 load 進來之後就會綁到 $this 上面, 比如說: $this->load->model('curl'); $this->curl->xxx(); 就不需要用傳統的 require_once __dir__ ."/curl.php"; $curl = new curl();
Demo Time I'll show you controller, model, view
Library 有些時候你會想用別人寫的 Library , 但他不是用 CI 這套 Framework ,怎麼辦? 沒關係,
CI 可以讓你使用別人寫的 Library , 用法一樣輕鬆寫意。 $this->load->library('phpmailer'); $this->phpmailer->send('
[email protected]
');
CI Session CI 製作團隊似乎覺得 php native 的 session 不是很好用,所以自己設計了一套 session
, 完全使用 cookie 加密存放資料, 也可以選擇使用 db 存放。 $this->session->set_userdata('id', 'ensky'); echo $this->session->userdata('id'); // ensky
CI Session Note: 如果有個 session 沒有被設定過 (!isset) 的話 不會噴錯誤,他會是 False
有個 flash_data 機制頗好用, 若某個 session 只想保留給下一頁使用, 就可以用 flashdata 來存 $this->session ->set_flashdata('errmsg', 'error!'); <?=$this->session->flashdata('errmsg')?>
Database • 基本上就完全是我前幾天講的 Active Record $result = $this->db->select('*') ->from('user') ->where('id',
'ensky') ->get()->result_array(); $insert_data = ['id'=>'ensky', 'gender'=>'boy']; $this->db->insert('user', $insert_data);
Other feature • Form Validation – 可以讓你很快的做表單輸入格式驗證 比如說密碼長度大於 blablabla 或者是這欄只能是數字,諸如此類。
http://www.codeigniter.org.tw/user_guide/libraries /form_validation.html
Other feature • input class – 使用者的資料向來都要視為很不安全, 比方說 get, post,
cookie 等資訊, ci 就把所有使用者的 input 包成一個 class , 可以用類似方式取用 $id = $this->input->post('id'); $cookie_id = $this->input->cookie('id');
DEMO!
Reference • 絕對強大的中文手冊 http://www.codeigniter.org.tw/user_guide/
More Decks by openwebschool
See All by openwebschool
09 - Node.JS - OpenWebSchool
openwebschool
1
400
07 - Javascript - OpenWebSchool
openwebschool
3
350
08 - js frontend & jQuery - OpenWebSchool
openwebschool
3
300
05 - MySQL - OpenWebSchool
openwebschool
1
280
06 - PHP & MySQL - OpenWebSchool
openwebschool
1
300
03 - PHP II - OpenWebSchool
openwebschool
2
410
04 - CSS - OpenWebSchool
openwebschool
4
370
01 - W3 intro - OpenWebSchool
openwebschool
3
260
02 - PHP I - OpenWebSchool
openwebschool
3
280
Other Decks in Programming
See All in Programming
小さなアプリを作って公開するまで
onozaty
0
250
正解の無い問題を 「できるだけ上手く」解く ヒューリスティックで感性を磨こう。〜現実課題をプログラミングで解きあうコミュニティについて〜
allegrogiken
0
120
はじめてのPLATEAU
nokonoko1203
1
670
特急いなほ as a Workflow / 20261107jaws-festa-2026-akita-lt
kasacchiful
PRO
0
110
AI Augmentation Techniques with Liberty
ivargrimstad
1
570
protojson における文字化けの扱い
kazuki1023
0
470
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
160
Compiling Your Language to WasmGC & Friends
tanishiking
0
170
DroidKnights 2026 Android A2UI Jetpack Compose 기반의 안전한 AI UI 렌더링 - 박제창
itsmedreamwalker
1
260
AI駆動開発4年目の現在地(『あなたはFDEになるべきか?』イベント資料)
kumaiu
0
700
joyful-journey-spring-boot.pdf
hollycummins
0
120
ローカルLLMの基本と近況 2611 / Local LLM basics and recent situation 2611
kishida
3
660
Featured
See All Featured
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
70
69k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.3k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
610
YesSQL, Process and Tooling at Scale
rocio
174
15k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
590
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
360
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
530
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
What the history of the web can teach us about the future of AI
inesmontani
PRO
3
830
Code Reviewing Like a Champion
maltzj
529
40k
Side Projects
sachag
456
43k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
121
130k