SpeedPHP架構學習-.基礎及MVC理解_PHP教程

來源:互聯網
上載者:User
Controller為控制器檔案。程式從index.php開始執行

[php]
define("SP_PATH",dirname(__FILE__)."/SpeedPHP");
define("APP_PATH",dirname(__FILE__));
$spConfig = array(
"db" =>array(
'host' =>'localhost',
'login' =>'root',
'password' =>'root',
'database' =>'test',
),
'view' => array(
'enabled' =>TRUE,
'config'=>array(
'template_dir'=> APP_PATH.'/tpl',
'compile_dir'=> APP_PATH.'/tmp',
'cache_dir'=> APP_PATH.'/tmp',
'left_delimiter'=> '<{',
'right_delimiter'=> '}>',
),
)
);
require(SP_PATH."/SpeedPHP.php");
spRun();
上述程式定義了App和SP的路徑,載入了資料庫和視圖層的配置,載入SP的核心庫檔案,最後運行整個系統。以上程式運行起來時,會首先到Controller目錄下執行main類下的index方法。main類的程式如下:

[php]
class main extends spController
{
function index(){
$tpl = $this->spArgs("tpl","green");
$guestbook =spClass("guestbook");
$this->results= $guestbook->findAll();
$this->display("{$tpl}/index.html");
}
function write(){
$guestbook =spClass("guestbook");
$newrow =array(
'name'=> $this->spArgs('name'),
'title'=> $this->spArgs('title'),
'contents'=> $this->spArgs('contents'),
);
$guestbook->create($newrow);
echo "return";
}
}
從index.php過來預設調用main方法的index函數,在本例中這個函數首先設定模板名參數(tpl)。再建立一個model。使用model的findall方法,尋找全部資料庫資訊。最後使用tpl模板顯示結果。上述控制器程式,必須繼承自spController類,方法名就是調用的action名。在程式中顯式調用時路徑為index.php?c=main&a=write。Model如下所示,是和資料庫表同名的類檔案。這個model必須繼承自spModel,同時設定了主鍵和表名屬性。

[php]
class guestbook extends spModel
{
var $pk = "id"; //每個留言唯一的標誌,可以稱為主鍵
var $table ="guestbook"; // 資料表的名稱
}
在tpl模板目錄裡的index.html檔案下使用如下所示程式,格式化輸出結果。

[html]
<{foreach from=$results item=one}>

<{$one.title}>


<{$one.name}>:
<{$one.contents}>


<{/foreach}>

http://www.bkjia.com/PHPjc/477933.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477933.htmlTechArticleController為控制器檔案。程式從index.php開始執行 [php] ?php define(SP_PATH,dirname(__FILE__)./SpeedPHP); define(APP_PATH,dirname(__FILE__)); $spConfig = array( db =arr...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.