FuelPHP 模組/HMVC筆記
FuelPHP對HMVC敘述不明不白,附帶一提那個V還是有問題的(後來新版架構已改成presenter)
app/config裡面的'module_paths' 項目要做好路徑設定,不過不是設自己的名稱
而是內定的APPPATH.'modules'.DS,
module裡面最底限要有類似fuelphp的架構
因為module和package的差距就是module"可以路由"
也就是說,如果執行netpath/mymodule/moduleincontroller/index之類的路徑
我們是可以像自己本身專案的PHP一樣使用的
然後實際上用的方式,可以用HMVC直接請求回傳結果,拿來給原本的主程式使用
首先我們模組裡面的控制器使用以下程式:
namespace hellomodule;
Class Controller_Hellomodule extends \Controller{
public function action_index(){
echo Model_mymodel::hellol();
if( ! \Request::is_hmvc()){
echo 'Normal request!';
}
else{
echo 'HMVC request!';
}
}
}
模組裡面的模型(註:module裡面的model)使用以下的程式:
namespace hellomodule;
class Model_mymodel extends \Model{
public static function hellol(){
return 'I already hate hello world!<br/>';
}
}
此時我們直接連線到hellomodule/hellomodule/index
將得到:
I already hate hello world!
Normal request!
但是如果我們在原本的控制器用一個HMVC式的請求
$temptext = Request::forge('hellomodule/hellomodule/index',false)->execute();
此時$temptext 裡面的回傳內容如果被echo出來,將會是:
I already hate hello world!
HMVC request!
沒有留言:
張貼留言