星期二, 十二月 11, 2007

CakePHP 入門、安裝 及 其它設定和準備工作

CakePHP 總算最近有花時間好好玩一下, 來做個一連串的筆記吧~

首先要使用 CakePHP 前, 第一步先了解何謂 MVC(Model-view-controller):

  • Model: 檢查資料型態/驗證, 與 DB 溝通(儲存/查詢) 等.
  • Controller: 程式的流程, 商業邏輯等. 是 Model 與 View 的中間層
  • View: 頁面瀏覽, 使用者看到的頁面就是這邊.

再來就是下載 CakePHP 來安裝看看囉~

官方網站: CakePHP : The PHP Rapid Development Framework

下載後, 直接解壓縮可見如下(1.1.xx 版) 三個目錄:(tar zxvf cake_1.1.18.5850.tar.gz)

  • app: 自己寫的程式放在這邊.
  • cake: Cake的核心函式庫, 升級的話, 只需要將此目錄的程式覆蓋掉即可.(此目錄的東西都不應該去碰他)
  • vendors: 放置其它外部函式庫的地方.
  • docs: 就基本說明文件等.

再來主要看的就是 app 的目錄內容:(下述參考自: CakePHP 使用手冊+留言板(繁體中文版)  - 基礎觀念)

  • config: DB/ACL 等設定檔
  • controllers: Controllers 控制頁面流程, 裡面檔名都會是 TABLEs_controller.php
    • components: 完整的元件(Components)放這邊.
  • index.php: 此隻會可以把 app 當 DocumentRoot, 不然直接將 DocumentRoot 指到 app/webroot 去也可以.
  • models: Models 與 DB 溝通的程式.
  • plugins: Plugins
  • tmp: 暫存資料
  • vendors: 放置其它外部函式庫的地方
  • views: Views
    • elements: Element 比較小的 view 模組
    • errors: 自訂的錯誤回報網頁
    • helpers: Helpers
    • layouts: 網頁頁面的預設 Layout(.thtml)(ex: default.thtml, 如果頁面沒有任何 template, 預設會 load default.thtml), cake 1.2 => default.ctp(檔名改用ctp)
    • pages: 固定內容的View
    • 註: 其它目錄: 如果有自己的 view modules, 就會在這邊另外建立, 預設目錄會是: TABLEs(TABLE NAME + 複數)
  • webroot: 程式的 DocumentRoot
    • css
    • files
    • img
    • js

解壓縮完第一個要設的是 app/tmp 的目錄, 懶人設法是 chmod 777 app/tmp 即可, 如果此資料夾會有多人一起使用, 可以考慮把要一起寫的人加入 www-data Group(Debian) 中, 然後 chown -R www-data:www-data *, 再到 cake 根目錄跑下面兩行:

sudo find . -type d -exec chmod 775 {} ";"
sudo find . -type f -exec chmod 664 {} ";"

這樣子基本上就可以了, 主要有幾點要記得的:

  • Module: 檔案命名用 單數, ex: modules/post.php
  • Controller: 檔案命名用 複數, 並要加 _controoler, ex: controllers/posts_controller.php
  • Views 要建目錄, 且 目錄命名用 複數, 裡面的檔案都是 .thml 結尾, 目錄裡的檔案命名要跟 Module function 對應, ex: views/posts/index.thtml (modules/post.php 要有 function index())

預設在寫的時後, 會發現 template 都是固定有 CakeXXX 的, 然後又找不到要去哪邊改, 主要是因為 views/layouts/default.thtml 造成的, 此檔案不存在的話, 不知道會去哪抓預設值, 所以可以跑下面的指令, 先把 default.thtml 複制一份, 就可以改 default.thtml 來做自己的頁面囉~

cp ../cake/libs/view/templates/layouts/default.thtml views/layouts/default.thtml (在 app 目錄下這樣子做, 從 layouts cp default 的 template 過來)

因為 views 都是 *.thtml, 所以 vim 也設定一下, 讓 *.thtml 跟解析 php 一樣的顏色, 只要 vim ~/.vim/filetype.vim, 內容如下:

" my filetype file
if exists("did_load_filetypes")
    finish
endif
augroup filetypedetect
    au! BufRead,BufNewFile *.thtml           setfiletype php
    au! BufRead,BufNewFile *.ctp              setfiletype php
    au! BufRead,BufNewFile *.inc               setfiletype php
augroup END

這樣子基本的環境設定, 和開發環境就大概完成了, 晚點再來寫下一步該怎麼做~

相關網頁:

相關標籤

this is comment icon how about Zend Framework? [回覆]

I've try Zend Framework, and it's very nice for MVC structure~

but any way, Cake is also nice too

Comment by fychao (12/11/2007 19:02)

this is comment icon 回 fychao [回覆]

Mmm... Zend 暫時還先不想用, 因為使用的人還不夠多, 文件也比較少.
特別是我剛要摸 Zend 的時後, 發現他沒有最重要的 Hello World 教學, 後來就沒有理他了, 不過我蠻期待 Zend 的, 畢竟是官方套件, 等等看囉 :)

Comment by Tsung (12/11/2007 20:50)

this is comment icon vimrc [回覆]

vim 設定文中說的 *.tcp 應該是 *.ctp ? ;-)

Comment by Rex Tsai (12/24/2007 16:50)

this is comment icon 回 Rex Tsai [回覆]

嗯嗯, 寫錯了, 感謝指正 Orz..

Comment by Tsung (12/24/2007 18:15)

this is comment icon 請問安裝在XP上該注意什麼? [回覆]

您好,請問安裝在XP上該注意什麼?我安裝過程截圖: http://picasaweb.google.com.tw/nickalbum12/20080111
Comment by Nick (01/11/2008 12:15)

this is comment icon 回 Nick [回覆]

XP 我想應該只要注意 app/tmp 要可以寫入, 這樣子應該就好了吧~ :)

Comment by Tsung (01/11/2008 19:22)

this is comment icon symfony [回覆]

我最近看到symfony: http://www.symfony-project.org/好像也是比较容易的framework php develop,这两个哪个更方便呢?
Comment by zxp (01/30/2008 18:16)

this is comment icon 回 zxp [回覆]

Mmm... symfony 非常的 java style, 要設蠻多 xml 的設定, 這個就看你喜歡寫什麼.
如果喜歡寫 java, 或許您可以考慮用用 symfony, 如果不是的話, 我想, 你應該會比較喜歡用 CakePHP. :)

Comment by Tsung (01/30/2008 22:47)

this is comment icon 用途寫反了 [回覆]

app: Cake的核心函式庫
cake: 自己寫的程式放在這邊

用途寫反了

Comment by topaz566 (04/14/2008 12:18)

this is comment icon 回 topaz566 [回覆]

感謝提醒, 已修正.. Orz..

Comment by Tsung (04/15/2008 08:29)
Add this page to del.icio.us Add this page to Yahoo Taiwan's bookmark Add this page to MyShare

發表迴響

標題

內容 (限制 1000 字)

暱稱

電子郵件

個人網頁


 authimage


PS: 若無法留言, 請先確認是否有打開 JavaScript, 造成您的困擾, 實在萬分對不起 Orz...(如果無法留言, 勞煩可以發信給我好嗎? 謝謝.)