一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

thinkphp 中$this->post(),$this->_get()函数介绍

时间:2015-01-23 编辑:简简单单 来源:一聚教程网

1.继承单一控制器

我们在写thinkphp控制器的时候,都是单独的控制器文件,在实际的项目中每个控制器可能都需要相关同样的验证,这种情况我们就可以的使用公共的控制器里写入对应的验证,其他控制器直接继承就好了这里面thinkphp.12为例子

/**
*公共控制器
*/
classCommonAction extendsAction {
publicstatic$global;
publicstatic$model;
publicfunction__construct(){
self::$global=getGolVal();
//dump(self::$global);
$navList=getNavlist(0,6);
$this->assign('list',$navList);
$this->assign('g',self::$global);
}
publicfunctionerror(){
$this->display('404');

/**
*
* Enter description here ...
*/
publicstaticfunctionnavLists($id,$type=0){
$id=intval($id);
self::$model=M('Category');
if($type==0){
$leftList=self::$model->where("parent_id=0 and mark=1 and type=0 ")->select();
}elseif($type==1){
$leftList=self::$model->where("id={$id} and mark=1 ")->find();
if($leftList['parent_id']==0){//顶级栏目
$leftList=self::$model->where("parent_id={$id} and mark=1 ")->select();
if(!is_array($leftList)){//顶级栏目 没有的子栏目话就显示
$leftList=self::$model->where("id={$id} and mark=1 ")->find();
$tempList=array();
$tempList[]=$leftList;
$leftList=$tempList;
}
}else{
$tempList=array();
$tempList[]=$leftList;
$leftList=$tempList;
}
}
/*dump($leftList);
dump(self::$model->getLastSql());*/
return$leftList;
}
}


额外的小知识,虽然在thinkphp中M()函数实例化模型,返回是一个静态变量,但是M方法却不是静态调用的,所以我们在多次调用M()时候尽量多使用静态属性,以提高好的效率。action操作里面尽量使用局部函数,减少资源消耗。

/**
*other控制器
*/
classOtherAction extendsCommonAction {
//code
}

2.对于post,get传来的值可以使用$this->_post();$this->_get();

$this->_post();$this->_get();

这个函数默认就会使用htmlspecialchars()进行过滤,不用手动过滤。哈哈,省了不少功夫吧

在thinkphp3.1.3中有一个新的函数I();直接接收表单数据,并默认为htmlspecailchars();过滤这个函数有这些字段 I('需要接收的表单名','如果数据为空默认值','使用的函数处理表单数据');

U();函数是输出地址

U('操作名','array()参数','伪静态后缀名',是否跳转,域名)

热门栏目