Heim > Backend-Entwicklung > PHP-Tutorial > 控制器基类

控制器基类

WBOY
Freigeben: 2016-07-25 08:47:57
Original
1389 Leute haben es durchsucht
非常简单实用的控制器基类
  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 = '';
  135. $wrap_end = ''.$wrap.'>';
  136. }else{
  137. $wrap_start = $wrap_end = '';
  138. }
  139. if($this->isAllow($ct,$ac))
  140. echo $wrap_start,'',$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 include $this->staticFile;
  165. exit;
  166. }
  167. }
  168. }
  169. }
  170. //生成url
  171. protected function url($ct='',$ac='',$param = array(),$module='')
  172. {
  173. return $GLOBALS['app']->url($ct,$ac,$param,$module);
  174. }
  175. //url跳转
  176. protected function redirect($ct='',$ac='',$param = array())
  177. {
  178. header('location:'.$this->url($ct,$ac,$param));
  179. exit();
  180. }
  181. //url跳转
  182. protected function redirectUrl($url)
  183. {
  184. header('location:'.$url);
  185. exit();
  186. }
  187. //获取back redirect url
  188. protected function getBru()
  189. {
  190. return $_COOKIE[util::c('bru_cookie_name')]?$_COOKIE[util::c('bru_cookie_name')]:$this->url();
  191. }
  192. //是否是ajax请求
  193. protected function isAjax()
  194. {
  195. if(isset( $_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
  196. return true;
  197. return false;
  198. }
  199. //返回json数组
  200. protected function returnJson($data)
  201. {
  202. echo json_encode($data);
  203. exit();
  204. }
  205. //GET
  206. protected function _G($name)
  207. {
  208. return isset($_GET[$name])?util::sanitize($_GET[$name]):'';
  209. }
  210. //POST
  211. protected function _P($name)
  212. {
  213. if(!isset($_POST[$name]) || (is_string($_POST[$name]) && mb_strpos($_POST[$name],'请输入',0,'gbk') === 0)){
  214. return '';
  215. }else{
  216. return util::sanitize($_POST[$name]);
  217. }
  218. }
  219. //REQUEST
  220. protected function _R($name)
  221. {
  222. return isset($_REQUEST[$name])?util::sanitize($_REQUEST[$name]):'';
  223. }
  224. }
复制代码


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage