BaseController.class.php初始化控制器代码信息
1 <?php 2 3 namespace Addons\Card\Controller; 4 5 use Home\Controller\AddonsController; 6 7 class BaseController extends AddonsController { //继承AddonsController类 8 function _initialize() { //定义初始化函数 9 $controller = strtolower ( _CONTROLLER ); //将控制器的名称转成小写 10 11 $res ['title'] = '会员卡制作'; //定义数组res的title值 12 $res ['url'] = addons_url ( 'Card://Card/config' ); //定义数组res的url值,addons_url()获取插件对应方法的函数 13 $res ['class'] = $controller == 'card' ? 'current' : ''; //定义数组res的url值 14 $nav [] = $res; //将数组res交给nav 15 16 $res ['title'] = '会员管理'; //定义数组res的title值 17 $res ['url'] = addons_url ( 'Card://member/lists' ); //定义数组res的url值 18 $res ['class'] = $controller == 'member' ? 'current' : ''; //定义数组res的url值 19 $nav [] = $res; //将数组res交给nav 20 21 $res ['title'] = '通知管理'; //定义数组res的title值 22 $res ['url'] = addons_url ( 'Card://notice/lists' ); //定义数组res的url值 23 $res ['class'] = $controller == 'notice' ? 'current' : ''; //定义数组res的url值 24 $nav [] = $res; //将数组res交给nav 25 26 $res ['title'] = '优惠卷'; //定义数组res的title值 27 $res ['url'] = addons_url ( 'Card://coupons/lists' ); //定义数组res的url值 28 $res ['class'] = $controller == 'coupons' ? 'current' : ''; //定义数组res的url值 29 $nav [] = $res; //将数组res交给nav 30 31 $this->assign ( 'nav', $nav ); //将数组nav分配给模板 32 33 $config = getAddonConfig ( 'Card' ); //获取插件的配置数组 34 $config ['background_url'] = $config ['background'] == 11 ? $config ['background_custom'] : ADDON_PUBLIC_PATH . '/card_bg_' . $config ['background'] . '.png'; //设置config数组中背景图片的url 35 $this->assign ( 'config', $config ); //将数组config分配给模板显示 36 //dump ( $config ); 37 //dump(get_token()); 38 } 39 }
MemberController.class控制器代码
1 <?php 2 3 namespace Addons\Card\Controller; 4 5 use Addons\Card\Controller\BaseController; //引入初始化类BaseController 6 7 class MemberController extends BaseController { //继承初始化类BaseController 8 var $model; //定义变量model; 9 function _initialize() { //定义初始化函数 10 $this->model = $this->getModel ( 'card_member' ); //使用getModel()函数查询数据库中model表中查询card_member信息,返回数组并交给模板 11 parent::_initialize (); //调用父类初始化 12 } 13 // 通用插件的列表模型 14 public function lists() { 15 // 不显示增加按钮 16 // $this->assign ( 'add_button', false ); 17 18 $map ['token'] = get_token (); //获取当前用户的token,交给数组map 19 session ( 'common_condition', $map ); //设置session值 20 21 parent::common_lists ( $this->model ); //显示该模型列表数据 22 } 23 24 // 通用插件的编辑模型 25 public function edit() { 26 parent::common_edit ( $this->model ); 27 } 28 29 // 通用插件的增加模型 30 public function add() { 31 parent::common_add ( $this->model ); 32 } 33 34 // 通用插件的删除模型 35 public function del() { 36 parent::common_del ( $this->model ); 37 } 38 }
上面控制器中common_edit 函数代码阅读
1 <?php 2 public function common_edit($model = null, $id = 0, $templateFile = '') { //什么函数common_edit,默认参数$model为空,$id为0,$templateFile为空 3 is_array ( $model ) || $model = $this->getModel ( $model ); //若$model不存在或不是数组,用getModel函数查询数据库,并给$model赋值 4 $id || $id = I ( 'id' ); //若$id不存在,给$id赋值为传过来的值 5 6 // 获取数据 7 $data = M ( get_table_name ( $model ['id'] ) )->find ( $id ); //用get_table_name函数获取表名,并查该id的数据 8 $data || $this->error ( '数据不存在!' ); //若无数据,报错 9 10 $token = get_token (); //获取token 11 if (isset ( $data ['token'] ) && $token != $data ['token'] && defined ( 'ADDON_PUBLIC_PATH' )) { //判断数据库中token是否存在,若存在是否与传过来的token对比,并检查常量ADDON_PUBLIC_PATH是否定义 12 $this->error ( '非法访问!' ); //抛出错误 13 } 14 15 if (IS_POST) { 16 $Model = D ( parse_name ( get_table_name ( $model ['id'] ), 1 ) ); //实例化模型 17 // 获取模型的字段信息 18 $Model = $this->checkAttr ( $Model, $model ['id'] ); //验证$Model 19 if ($Model->create () && $Model->save ()) { //判断并保存数据 20 $this->_saveKeyword ( $model, $id ); 21 22 $this->success ( '保存' . $model ['title'] . '成功!', U ( 'lists?model=' . $model ['name'], $this->get_param ) ); 23 } else { 24 $this->error ( $Model->getError () ); 25 } 26 } else { 27 $fields = get_model_attribute ( $model ['id'] ); //获取数据并缓存 28 29 $this->assign ( 'fields', $fields ); 30 $this->assign ( 'data', $data ); 31 $this->meta_title = '编辑' . $model ['title']; 32 33 $templateFile || $templateFile = $model ['template_edit'] ? $model ['template_edit'] : ''; 34 $this->display ( $templateFile ); //调用模板显示 35 } 36 }