2012年8月23日

PHP MVC 網站開發模組化利器 CodeIgniter + Sparks


CodeIgniter 是 PHP 的 MVC 網站開發框架,它提供一個基礎的架構,讓網站剛起步就有良好的各項機制,包括 URL Routing(支援友善網址)、Models-Views-Controllers、Class Libraries(可自訂擴充的函式庫)、Drivers(Caching、JavaScript、Database抽象層)、Helpers、Hook 及 Error Handler 等。

CodeIgniter is an Application Development Framework - a toolkit - for people who build web sites using PHP. Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries. CodeIgniter lets you creatively focus on your project by minimizing the amount of code needed for a given task.

CodeIgniter 的 Helper 已提供不少常用函式,例如:CAPTCHA、Mail、XML、Form...等。

對於網站開發實務,開發者需要在 CodeIgniter 的架構及規則下,擴充及撰寫自己的程式。對於可能「重複使用(reuse)」的程式碼,有經驗的開發者會考慮寫成 Libraries 形式,以方便維護、重複利用開發類似網站,或者熱心地開放源碼分享給其他開發者使用。

為了方便程式碼重複使用,程式檔數量多時,模組化的設計就顯得重要。在網站程式中我們會希望將不同用途的程式檔,分別歸類在不同的模組,但是對 CodeIgniter 來說,程式檔可能分別散佈在不同資料夾下,並不會集中在某個模組的專用資料夾。

最簡易但比較笨的處理方式,是利用 shell scripts 複製、打包指定的檔案和資料夾。

比較好的作法是搭配 Package Management(套件管理)的工具實作,例如 PHP 官方提供的 PEAR(PHP Extension and Application Repository)工具,當需要用到 XML_RPC2 這個套件時,只要用「pear install XML_RPC2」安裝即可。

CodeIgniter 也有類似的套件管理,但是並非內建在 CodeIgniter 的官方發行版,必須額外加裝 Sparks 工具。

Sparks is a package management system for Codeigniter
http://getsparks.org/

為什麼要另外發展 Sparks 而不是利用 PHP 的 PEAR 呢?CodeIgniter 有以下的說明

PHP has an existing package management system, PEAR. Ideally a framework specific package manager would simply wrap around the language equivalent. Unfortunately with PEAR’s odd approach to packaging that isn’t feasible. It is regretable that the PHP standard has not enjoyed the success that gems, easy_install, and npm have seen.

Sparks 用在 PHP 的網站開發,有點類似 RubyGems、NPM(Node.js);但它是針對 CodeIgniter MVC Framework 設計,較不適合直接用於一般的 PHP 程式。

Sparks 的發展和 CodeIgniter 有緊密的關係,因為 CodeIgniter 的目標是成為輕薄短小速度快的 PHP Framework,所以內建功能與擴充函式庫並不打算包山包海;但是在講究開發迅速的現代,開發者又期待有更多現成的函式庫可加以利用,這些 CodeIgniter 開發者期望有但又不能內建的功能,就會由 Sparks 的第三方社群提供來源。

建議使用版本較新的 CodeIgniter(簡稱 CI),在 CI 專案的根目錄下,執行 Sparks 安裝指令:

php -r "$(curl -fsSL http://getsparks.org/go-sparks)"

這道指令會在根目錄下增加「sparks」、「tools」資料夾,以及「application/core/MY_Loader.php」設定檔。

安裝完成後,接下來就可以使用 Sparks 的指令安裝新模組,例如:

php tools/spark install -v1.0.0 example-spark

如果不指定版本編號(1.0.0),就會自動辨別並安裝最新版。

MY_Loader.php 的作用就是為 CI 程式增加 spark 載入函式,因此在 Controller 函式的程式碼,可以利用 spark function 載入指定的 Spark 模組。

$this->load->spark('example-spark/1.0.0');

以「example-spark」範例模組來說,它的功能就是提供 printHello 這個函式給開發者測試,這個程式會在 HTML Output 印出 Hello 字樣。

$this->example_spark->printHello();

對於經常使用的模組,可以直接在 CI 的自動載入設定(config/autoload.php)增加:

$autoload['sparks'] = array('example-spark/1.0.0');

除了在 Sparks 網站搜尋套件,也可利用指令:

php tools/spark search test

列出已安裝的套件:

php tools/spark list

其他指令可以用 help 列出:

php tools/spark help

對於想製作 Spark 模組並分享回饋給社群的開發者,Spark 支援 GitHub 及 BitBucker 兩個開放源碼專案平台,這個設計相當值得稱許。

例如這個 amazon-sdk 模組,就是利用 GitHub 托管原始碼:


模組的檔案結構需要依循 Spark 的規範,其中 spark.info 就是此模組的資訊。


CodeIgniter + Spark 可以讓 PHP 開發者更迅速打造網站,並方便將模組化的程式碼分享回饋給社群。

@本文作者 lyhcode

沒有留言:

張貼留言

lyhcode by lyhcode
歡迎轉載,請務必註明出處!