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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
390
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
CSC307 Lecture 09
javiergs
PRO
1
830
KIKI_MBSD Cybersecurity Challenges 2025
ikema
0
1.3k
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
990
AI時代の認知負荷との向き合い方
optfit
0
140
「ブロックテーマでは再現できない」は本当か?
inc2734
0
100
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
1.1k
AIで開発はどれくらい加速したのか?AIエージェントによるコード生成を、現場の評価と研究開発の評価の両面からdeep diveしてみる
daisuketakeda
1
970
FOSDEM 2026: STUNMESH-go: Building P2P WireGuard Mesh Without Self-Hosted Infrastructure
tjjh89017
0
150
Patterns of Patterns
denyspoltorak
0
1.4k
Oxlint JS plugins
kazupon
1
640
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
220
CSC307 Lecture 04
javiergs
PRO
0
650
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.7k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
61
52k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Writing Fast Ruby
sferik
630
62k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
170
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.7k
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Thoughts on Productivity
jonyablonski
74
5k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
64
Building AI with AI
inesmontani
PRO
1
680
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/