基础控制器【grace】源码及说明
所有的自定义控制器必须继承 grace 基础控制器,基础控制器内置了一些常用的方法以便您的开发!您也可以扩展 grace 来实现更为便捷的开发 ^_^<?php
class grace{
public $gets; //url 参数集合
public $tableName = null; //绑定数据表名称
public $tableKey = null; //对应数据表的主键
public $db; //数据表操作对象
public $order = null; //查询排序
public $postFilter = true; //是否过滤 post 内的攻击性字符
public $pageInfo = array('', '', ''); //网站开发是页面的标题、关键字、描述
public function __construct(){}
//初始化函数
public function __init(){}
//视图展示函数,系统会自动展示视图,您也可以使用此方法手动调用视图
public function display($tplName = null){}
//语言包设置函数
protected function setLang($langType){}
//json 结果输出函数,会终止运行,参数 1 : 数据内容,参数 2 : 状态【默认 ok】
protected function json($data, $type = 'ok'){
pgExit(json_encode(array('status' => $type, 'data' => $data)));
}
//根据数据表配置获取数据列表【带有分页】
protected function dataList($everyPagerNum = 20, $fields = '*'){
}
//根据数据表配置获取数据【利用主键获取】
protected function getDataById(){
if(empty($this->gets[0])){return null;}
return $this->db->where($this->tableKey .' = ?', array(intval($this->gets[0])))->fetch();
}
//表单的默认值填充【依赖 jquery】
protected function getDefaultVal($exception = array()){
if(empty($this->gets[0])){return null;}
//......
return $data;
}
//跳转到分组首页并终止程序运行
public function skipToIndex(){
header('location:'.PG_SROOT);
exit;
}
//检查 $this->gets[xx] 是否存在或为空,为空则为其赋指定的值
protected function initVal($key, $val = ''){
if(empty($this->gets[$key])){$this->gets[$key] = $val;}
}
//检查 $this->gets[xx] 是否存在或为空,为空则为其赋指定的值,不为空则进行整数规范
protected function intVal($key, $val = 0){
if(empty($this->gets[$key])){
$this->gets[$key] = 0;
}else{
$this->gets[$key] = intval($this->gets[$key]);
}
}
}