thinkPHP 空模組和空操作、前置操作和後置操作 詳細介紹(十四)_PHP教程

來源:互聯網
上載者:User

thinkPHP 空模組和空操作、前置操作和後置操作 詳細介紹(十四)


本章節:介紹 TP 空模組和空操作、前置操作和後置操作 詳細介紹

一、空模組和空操作
1、空操作
function _empty($name){
$this->show("$name 不存在 www.Bkjia.com");
}
2.空模組(EmptyAction.class.php的檔案)
class EmptyAction extends Action{
function index(){
//$this->show('

該要求方法不存在!

')
$city=M('City');
$arr=$city->select();
$this->assign('list',$arr);
$name=MODULE_NAME; //擷取當前模組名,手冊常量參考有一堆類似常量
//http://localhost/thinkphp/index.php/Index/moBanXuanRan
//模組名就是:Index
$this->display("City:$name");
}
}

當前模組下(控制器),調用其他模組下的方法:
//在CityAction控制器下調用IndexAction控制器下的方法
//直接new下,能後在找到對應方法即可
class CityAction extends Action{
public function tiaozhuan(){
$indexAction = new IndexAction();
$indexAction->index();
}
}
?>

二、前置操作和後置操作
解釋:www.Bkjia.com
比如:我現在在執行 http://localhost/thinkphp/index.php/Index/index index方法
前置方法:在執行index方法之前,執行的一些邏輯操作
後置方法:在執行完index方法後,執行的一些邏輯操作

例子:比如你現在做了個網站,但是訪問你這個網站的摸個方法時候必須登入,就可以用
前置和後置操作

1、前置操作: _before_操作名
2、後置操作: _after_操作名
class IndexAction extends Action{
public _before_index(){
//判斷,如果沒有登入就跳轉到首頁
//如果沒登入就跳轉到登入頁面
if(!isset($_SESSION['username']) || $_SESSION['username']==''){
$this->redirect('Login/index'); //跳轉到Login控制器下的index方法
}
}
public function index(){
$user = M('User');
$arr = $user->select();
$this->assign('list',$arr);
$this->display();
}

public _after_index(){
$this->show('這是index方法的後置操作!!');
}
}

http://www.bkjia.com/PHPjc/931646.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/931646.htmlTechArticlethinkPHP 空模組和空操作、前置操作和後置操作 詳細介紹(十四) 本章節:介紹 TP 空模組和空操作、前置操作和後置操作 詳細介紹 一、空...

  • 聯繫我們

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