ThinkPHP 學習之控制器 ( Controller )

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   sp   

/** * ThinkPHP version 3.1.3 * 部署方式:應用部署 
* 文內的 http://localhost/ 由實際主機地址代替
*/

入口檔案 index.php:

<?phpdefine(‘THINK_PATH‘,‘./ThinkPHP/‘);//定義項目名稱和路徑define(‘APP_NAME‘,‘Myapp‘);define(‘APP_PATH‘,‘./home/‘);define(‘APP_DEBUG‘,true);//載入架構入口檔案require(THINK_PATH."ThinkPHP.php");
index.php

訪問入口檔案 index.php ( http://localhost/index.php ) 後自動產生 ./home 目錄。

index 初始頁面的內容由 ./home/Lib/Action/IndexAction.class.php 檔案顯示:

<?php// 本類由系統自動產生,僅供測試用途class IndexAction extends Action {    public function index(){    $this->show(‘<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微軟雅黑"; color: #333;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>歡迎使用 <b>ThinkPHP</b>!</p></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>‘,‘utf-8‘);    }}
IndexAction.class.php

( 一 ) 控制器的調用

現在修改 IndexAction.class.php,使用 A 函數調用本項目控制器:

<?phpclass IndexAction extends Action {    public function test(){            $obj = A("Member");        $obj->user();    }}    

A 函數相當於 new,A("Member") 表示執行個體化本項目中的 Member 控制器,然後再調用 Member 控制器中的 user 方法。user 方法 ( MemberAction.class.php ):

<?phpclass MemberAction extends Action{    public function user(){            $this->display();    }}

因為在 Index 控制器 中的 test 動作調用了 Member 控制器的 user 方法 ( 因為此處的 user 動作已被執行個體化,因此此處不能成為動作,而是作為類的方法 ) 的 $this->display(),所以需要在當前動作 ( test 動作 ) 中輸出模板 ( ./home/Tpl/Index/test.html ) :

<!DOCTYPE html><html><head><meta charset="utf-8"><title>test</title></head><body>歡迎使用 ThinkPHP</body></html>
test.html

通過 PATHINFO 模式的 url ( http://localhost/index.php/index/test ,我的主機地址是127.0.0.26 ) 可以訪問 Index 控制器的 test 動作

 

可以使用 R 函數來代替 A 函數,R 函數可以在調用控制時指定調用方法 ( 動作 ),修改 IndexAction.class.php:

<?phpclass IndexAction extends Action {    public function test(){            $obj = R("Member/user"); //使用 R 函數調用本項目的控制器    }}

可以達到同樣的效果。

 

( 二 ) 空控制器與空動作

空控制器和空動作可以實現錯誤 404 的功能 ( 但是不能取代伺服器錯誤頁面處理機制,空控制器和空動作只能處理 MVC 架構內的頁面,而且也不能處理 URL REWRITE 自訂格式的頁面 )。

1、空控制器

當使用者訪問的 URL 不存在需要訪問的控制器時,可以使用空控制器,例如訪問 http://localhost/index.php/bbs,實際上不存在控制器 BbsAction,系統會給出錯誤 ( 需要在入口檔案 index.php 開啟 define(‘APP_DEBUG‘,true); ):

現在在項目目錄 ( ./home/Lib/Action ) 下建立控制器類檔案 EmptyAction.class.php :

<?phpclass EmptyAction extends Action{    public function index(){            $this->assign("msg","你所查看的欄目已經不存在");        $this->display("./Public/html/error.html");    }}

同時在 WEB 根目錄的 Public/html 下建立error.html,這樣訪問  http://localhost/index.php/bbs 出現的就是自訂的資訊內容。

 

2、空動作

相較空控制器定位到欄目,空動作則是定位到具體的頁面。如果 Index 控制器中不存在 user 動作,只需要在 Index 控制器中加入空動作,系統會把 _empty 動作代替 user 動作:

<?phpclass IndexAction extends Action {    public function test(){            $obj = R("Member/user");    }    public function _empty(){            $this->assign("msg","你所查看的頁面不存在");        $this->display("./Public/html/error.html");    }}

 

參考資料:《PHP MVC 開發實戰》

 

ThinkPHP 學習之控制器 ( Controller )

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.