thinkPHP 空模块和空操作、前置操作和后置操作 具体介绍(十四)

本章节:介绍 TP 空模块和空操作、前置操作和后置操作 具体介绍


一、空模块和空操作
1、空操作
function _empty($name){
$this->show("$name 不存在 <a href='__APP__/Index/index'>返回首页</a>");
}
2.空模块(EmptyAction.class.php的文件)
class EmptyAction extends Action{
function index(){
//$this->show('<p>该请求方法不存在!

</p>')
$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");
}
}

当前模块下(控制器),调用其它模块下的方法:
<?

php
//在CityAction控制器下调用IndexAction控制器下的方法
//直接new下,能后在找到相应方法就可以
class CityAction extends Action{
public function tiaozhuan(){
$indexAction = new IndexAction();
$indexAction->index();
}
}
?>

二、前置操作和后置操作
解释:
比方:我如今在运行 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方法的后置操作。!

');
}
}

原文地址:https://www.cnblogs.com/blfbuaa/p/7073816.html