当前位置:Gxlcms > PHP教程 > 控制器基类

控制器基类

时间:2021-07-01 10:21:17 帮助过:15人阅读

非常简单实用的控制器基类
  1. /**
  2. * @desc 控制器基类
  3. * @date 2013-05-06
  4. * @author liudesheng
  5. */
  6. defined('SYS_PATH') || die('访问非法');
  7. class controller
  8. {
  9. //当前控制器
  10. protected $_controller;
  11. //当前动作方法
  12. protected $_action;
  13. //权限数组
  14. protected $_permissions;
  15. //模板文件
  16. private $_layout = 'layout';
  17. //构造函数
  18. function __construct($controller,$action)
  19. {
  20. if('exception' != $controller){
  21. $this->_controller = $controller;
  22. $this->_action = $action;
  23. //登录检查和访问权限控制部分,登录页不需要验证
  24. $trust_action = util::c('trust_action');
  25. if(!isset($trust_action[$this->_controller]) || !in_array($this->_action,$trust_action[$this->_controller])){
  26. $this->login();
  27. //$this->privilege();
  28. }
  29. $this->init();
  30. }else{//异常处理
  31. $this->exception($action);
  32. }
  33. }
  34. //初始化方法,用于继承操作
  35. protected function init(){}
  36. //异常处理方法
  37. private function exception($msg)
  38. {
  39. $this->showErr($msg,$layout);
  40. }
  41. //验证登录
  42. private function login()
  43. {
  44. if(!$this->isLogin()){
  45. if($this->isAjax()){
  46. header('HTTP/1.1 403 Forbidden');
  47. header("Error-Json:{code:'login'}");
  48. exit();
  49. }else{
  50. $this->redirect('index','login');
  51. }
  52. }
  53. }
  54. //判断是否登录
  55. protected final function isLogin()
  56. {
  57. $auth = isset($_COOKIE['auth'])?$_COOKIE['auth']:'';
  58. $isLogin = false;
  59. if($auth){
  60. $info = trim(file_get_contents('check.txt'));
  61. if(strcmp($auth,md5('steve'.$info.util::c('login_auth_suffix'))) == 0){
  62. $isLogin = true;
  63. }
  64. }
  65. return $isLogin;
  66. }
  67. //验证权限
  68. private function privilege()
  69. {
  70. $this->getPermissions();
  71. if(!$this->isAllow()){
  72. if($this->isAjax()){
  73. header('HTTP/1.1 403 Forbidden');
  74. header( "Error-Json:{code:'access'}");
  75. exit();
  76. }else{
  77. $this->showErr('对不起,您没有此权限');
  78. }
  79. }
  80. }
  81. //获取权限信息
  82. protected final function getPermissions()
  83. {
  84. $privilege = $this->admin['privilege'];
  85. $permissions_priv = util::c('permissions',$privilege);
  86. if(!isset($permissions_priv['city'])){
  87. $this->cityPriv = 'all'; //为了简化列表查询,方便以后可能添加所有城市权限选择
  88. }else{
  89. unset($permissions_priv['city']);
  90. }
  91. foreach($permissions['common'] as $ct => $ac){
  92. if(isset($permissions_priv[$ct]) && 'all' == $permissions_priv[$ct])
  93. continue;
  94. if('all' == $ac)
  95. $permissions_priv[$ct] = 'all';
  96. else //这种情况必须是数组,节省资源,不做判断了
  97. $permissions_priv[$ct] = isset($permissions_priv[$ct])?array_merge($permissions_priv[$ct],$ac):$ac;
  98. }
  99. $this->_permissions = $permissions_priv;
  100. }
  101. //根据权限类型判断是否有权限
  102. protected final function isAllow($controller='',$action='')
  103. {
  104. if(!isset($this->_permissions))
  105. $this->getPermissions();
  106. $allow = false;
  107. $ct = $controller?$controller:$this->_controller;
  108. $ac = $action?$action:$this->_action;
  109. $permission_action = $this->_permissions[$ct];
  110. if($permission_action && ('all' == $permission_action || in_array($ac,$permission_action) || 'any' == $action))
  111. $allow = true;
  112. return $allow;
  113. }
  114. //错误信息页面
  115. protected function showErr($errMsg,$layout = null)
  116. {
  117. $this->title = "错误提示";
  118. $this->errMsg = $errMsg;
  119. $this->render('error',$layout);
  120. }
  121. //成功信息页面
  122. protected function showSucc($msg,$skipUrl,$skipPage,$layout = null)
  123. {
  124. $this->title = "成功提示";
  125. $this->msg = $msg;
  126. $this->skipUrl = $skipUrl;
  127. $this->skipPage = $skipPage;
  128. $this->render('success',$layout);
  129. }
  130. //显示有权限的链接
  131. protected function showPemissionLink($title,$ct,$ac,$param=array(),$wrap='')
  132. {
  133. if($wrap){
  134. $wrap_start = '<'.$wrap.'>';
  135. $wrap_end = '';
  136. }else{
  137. $wrap_start = $wrap_end = '';
  138. }
  139. if($this->isAllow($ct,$ac))
  140. echo $wrap_start,'url($ct,$ac,$param),'" >',$title,'',$wrap_end;
  141. }
  142. //视图解析方法
  143. protected function render($template = null,$layout = null)
  144. {
  145. !is_null($layout) && $this->_layout = $layout;
  146. !$template && $template = $this->_controller.'_'.$this->_action;
  147. ob_start();
  148. include(MODULE_PATH.'views/'.$this->_layout.'.tpl.php');
  149. $content = ob_get_clean();
  150. if($this->staticFile){
  151. file_put_contents($this->staticFile,$content);
  152. }
  153. echo $content;
  154. exit;
  155. }
  156. protected function showHtml($html,$expire=3600,$path='')
  157. {
  158. empty($path) && $path=ROOT_PATH;
  159. $this->staticFile = sprintf('%s%s.html',$path,$html);
  160. $mkhtml = intval($this->_G('mkhtml'));
  161. if(!$mkhtml){
  162. if(file_exists($this->staticFile)){
  163. $fmtime = filemtime($this->staticFile);
  164. if(time()-$fmtime < $expire && date('Ymd') == date('Ymd',$fmtime)){
  165. include $this->staticFile;
  166. exit;
  167. }
  168. }
  169. }
  170. }
  171. //生成url
  172. protected function url($ct='',$ac='',$param = array(),$module='')
  173. {
  174. return $GLOBALS['app']->url($ct,$ac,$param,$module);
  175. }
  176. //url跳转
  177. protected function redirect($ct='',$ac='',$param = array())
  178. {
  179. header('location:'.$this->url($ct,$ac,$param));
  180. exit();
  181. }
  182. //url跳转
  183. protected function redirectUrl($url)
  184. {
  185. header('location:'.$url);
  186. exit();
  187. }
  188. //获取back redirect url
  189. protected function getBru()
  190. {
  191. return $_COOKIE[util::c('bru_cookie_name')]?$_COOKIE[util::c('bru_cookie_name')]:$this->url();
  192. }
  193. //是否是ajax请求
  194. protected function isAjax()
  195. {
  196. if(isset( $_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
  197. return true;
  198. return false;
  199. }
  200. //返回json数组
  201. protected function returnJson($data)
  202. {
  203. echo json_encode($data);
  204. exit();
  205. }
  206. //GET
  207. protected function _G($name)
  208. {
  209. return isset($_GET[$name])?util::sanitize($_GET[$name]):'';
  210. }
  211. //POST
  212. protected function _P($name)
  213. {
  214. if(!isset($_POST[$name]) || (is_string($_POST[$name]) && mb_strpos($_POST[$name],'请输入',0,'gbk') === 0)){
  215. return '';
  216. }else{
  217. return util::sanitize($_POST[$name]);
  218. }
  219. }
  220. //REQUEST
  221. protected function _R($name)
  222. {
  223. return isset($_REQUEST[$name])?util::sanitize($_REQUEST[$name]):'';
  224. }
  225. }

人气教程排行