Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
11 - CodeIgniter - OpenWebSchool
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
openwebschool
August 22, 2012
Programming
1
350
11 - CodeIgniter - OpenWebSchool
openwebschool
August 22, 2012
Tweet
Share
More Decks by openwebschool
See All by openwebschool
09 - Node.JS - OpenWebSchool
openwebschool
1
390
07 - Javascript - OpenWebSchool
openwebschool
3
340
08 - js frontend & jQuery - OpenWebSchool
openwebschool
3
280
05 - MySQL - OpenWebSchool
openwebschool
1
250
06 - PHP & MySQL - OpenWebSchool
openwebschool
1
280
03 - PHP II - OpenWebSchool
openwebschool
2
400
04 - CSS - OpenWebSchool
openwebschool
4
360
01 - W3 intro - OpenWebSchool
openwebschool
3
250
02 - PHP I - OpenWebSchool
openwebschool
3
270
Other Decks in Programming
See All in Programming
Oxlintはいいぞ
yug1224
5
1.3k
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6k
AI巻き込み型コードレビューのススメ
nealle
1
190
Oxlint JS plugins
kazupon
1
890
SourceGeneratorのススメ
htkym
0
190
コマンドとリード間の連携に対する脅威分析フレームワーク
pandayumi
1
450
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
230
それ、本当に安全? ファイルアップロードで見落としがちなセキュリティリスクと対策
penpeen
7
3.9k
CSC307 Lecture 02
javiergs
PRO
1
780
2026年 エンジニアリング自己学習法
yumechi
0
130
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
200
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
370
Featured
See All Featured
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
54
Designing Powerful Visuals for Engaging Learning
tmiket
0
230
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
210
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
22k
Scaling GitHub
holman
464
140k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
120
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
710
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.2k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
240
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
240
Transcript
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/