Home  >  Article  >  Backend Development  >  thinkPHP 空模块跟空操作、前置操作和后置操作 详细介绍(十四)

thinkPHP 空模块跟空操作、前置操作和后置操作 详细介绍(十四)

WBOY
WBOYOriginal
2016-06-13 12:12:10781browse

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

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


一、空模块和空操作
1、空操作
function _empty($name){
$this->show("$name 不存在 返回首页");
}
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();
}
}
?>

二、前置操作和后置操作
解释:
比如:我现在在执行 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方法的后置操作!!');
}
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn