2007年5月15日 星期二

CakePHP使用手冊-插件(Plugin)

轉貼自 http://www.ezluk.org/

插件(Plugin)



CakePHP允許您將一堆model,controler和view打包在一起,讓別人以plugin的方式使用在自己的CakePHP應用程式裡。
有一個很棒的使用者管理模組?簡單的blog?或是好用的web service模組?將它們打包成CakePHP plugin,放到別人的應用程式裡吧。



應用程式和安裝的plugin間最主要的連繫在於應用程式的設定(資料庫連結設定等)。
除此之外,它在自己的小空間內運作,就好像只有自己是獨立的應用程式一樣。



第一節


建立Plugin



讓我們示範建立一個新的plugin,功能是訂pizza。還有什麼功能比這個範例更有用的呢?
開始吧,我們必需先在/app/plugins目錄內建立一個子目錄,準備將plugin 的檔案放在裡頭。
這個子目錄的名稱相當重要,會被用在很多地方,所以選一個好名字吧。
對這個範例來說,就讓它叫'pizza'好了。
最後目錄結構看起來會像這樣:




定Pizza plugin的目錄結構



/app
/plugins
/pizza
/controllers <- plugin controllers 放這兒
/models <- plugin models 放這兒
/views <- plugin views 放這兒
/pizza_app_controller.php <- plugin的 AppController,依據plugin的名字命名
/pizza_app_model.php <- plugin的 AppModel,依據plugin的名字命名



一般的應用程式不需要定義AppController和AppModel,但是plugin需要。
必需建立它們,plugin才會正常運作。
這二個特殊的類別是根據plugin的名字來命名,類別繼承自AppController和AppModel類別。
它們看起來像這樣:




Pizza Plugin AppController:
/app/plugins/pizza_app_controller.php



<?php



class PizzaAppController extends AppController

{

    //...

}



?>




Pizza Plugin AppModel: /app/plugins/pizza_app_model.php



<?php



class PizzaAppModel extends AppModel

{

    //...

}



?>



如果你忘了定義這二個特別的類別,CakePHP會送你一個"Missing Controller"錯誤。




第二節


Plugin的Controller



我們這個pizza plugin的controller會存放在/app/plugins/pizza/controllers
既然我們主要想追踪的是pizza訂單,那就得要有一個OrdersController。




雖然不是絕對必要,但我們還是強烈建議,plugin裡的controller最好取相對唯一的名稱,以免與使用的應用程式相衝。
其他程式內也非常可能會用UsersController,OrderController或ProductController等名稱:
所以你對controller的命名可能需要多一點創意,或把plugin的名稱當作類別的前置詞(像PizzaOrdersController)。




所以,我們把新的PizzaOrdersController放在/app/plugins/pizza/controllers裡,看起來像這樣:




/app/plugins/pizza/controllers/pizza_orders_controller.php



<?php



class PizzaOrdersController extends PizzaAppController

{

    var $name = 'PizzaOrders';



    function index()

    {

        //...

    }



    function placeOrder()

    {

        //...

    }

}



?>



注意這個controller是繼承plugin的AppController(叫PizzaAppController)而不是上層應用程式的AppController。




第三節


Plugin的Model



plugin的Model存放在/app/plugins/pizza/models目錄內。
我們已經為這個plugin定義了一個PizzaOrdersController,讓我們為這個controller建一個model,叫PizzaOrder(
類別名稱PizzaOrder有依我們的命名規則命名,也夠獨特,所以就讓它叫這個名字吧)。




/app/plugins/pizza/models/pizza_order.php



<?php



class PizzaOrder extends PizzaAppModel

{

    var $name = 'PizzaOrder';

}



?>



同樣的,這個類別繼承自PizzaAppModel而不是AppModel。




第四節


Plugin的View



View的行為和一般的應用程式一模一樣。只要將他們放在/app/plugins/[plugin]/views/目錄內。
對我們這個訂pizza的plugin來說,最少需要一個view對應PizzaOrdersController::index() action,檔名和目錄位置像這樣:




/app/plugins/pizza/views/pizza_orders/index.thtml



<h1>Order A Pizza</h1>

<p>Nothing goes better with Cake than a good pizza!</p>

<!-- An order form of some sort might go here....-->




第五節


使用plugin



現在你已經備妥一切,可以準備發布(這時,我們會建議再多一些額外的資料,如說名檔,SQL檔等)。



plugin被安裝在/app/plugins後,就可以透過URL /pluginname/controllername/action存取。
在訂pizza的範例中,就可以在/pizza/pizzaOrders 存取到PizzaOrdersController。



以下是在CakePHP中使用plugin的一些注意事項:





  1. 如果[Plugin]AppController和[Plugin]AppModel不存在,在嘗試存取plugin controller時,
    會得到"Missing Controller"的錯誤訊息。





  2. 可以用plugin的名字做一個預設的controller。
    如果有做這個controller,就可以透過/[plugin]/action存取。
    例如,有個叫'users'的plugin,內含名為UsersController的controller,就可以透過/users/add取用add這個action。
    但要確定沒有另一個叫AddController controller被放在[plugin]/controllers目錄內。


    譯註:存取users plugin的AddController的URL也是/users/add/[action]





  3. Plugin預設使用/app/views/layouts目錄下的layout。





  4. 可以在controller內使用$this->requestAction('/plugin/controller/action');與plugin進行內部溝通。




  5. 使用你使用requestAction,請確認controller和model名稱盡可能唯一。
    否則PHP就會送你一個"redefined class ..."的錯誤。





特別感謝Felix Geisendorfer提供本章節的內容。


沒有留言:

網誌存檔

關於我自己

Aspire freedom , Hope to do Soming make self complete ~