Home >Backend Development >PHP Tutorial >Controller base class

Controller base class

WBOY
WBOYOriginal
2016-07-25 08:47:571402browse
Very simple and practical controller base class
  1. /**
  2. * @desc controller base class
  3. * @date 2013-05-06
  4. * @author liudesheng
  5. */
  6. defined('SYS_PATH') || die('Access Illegal');
  7. class controller
  8. {
  9. //Current controller
  10. protected $_controller;
  11. //Current action method
  12. protected $_action;
  13. //Permission array
  14. protected $_permissions;
  15. //Template file
  16. private $_layout = 'layout';
  17. //Constructor function
  18. function __construct($controller,$action )
  19. {
  20. if('exception' != $controller){
  21. $this->_controller = $controller;
  22. $this->_action = $action;
  23. //Login check and access permission control part, login The page does not require verification
  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{// Exception handling
  31. $this->exception($action);
  32. }
  33. }
  34. //Initialization method, used for inheritance operations
  35. protected function init(){}
  36. //Exception handling method
  37. private function exception($ msg)
  38. {
  39. $this->showErr($msg,$layout);
  40. }
  41. //Verify login
  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. //Determine whether to log in
  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. //Verification Permissions
  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('Sorry, you do not have this permission ');
  78. }
  79. }
  80. }
  81. //Get permission information
  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'; //In order to simplify the list query, it is possible to add all city permissions in the future Select
  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 //This case must be an array , save resources and don’t make judgments
  97. $permissions_priv[$ct] = isset($permissions_priv[$ct])?array_merge($permissions_priv[$ct],$ac):$ac;
  98. }
  99. $this-> _permissions = $permissions_priv;
  100. }
  101. //Determine whether there is permission based on the permission type
  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. //Error message page
  115. protected function showErr($errMsg,$layout = null)
  116. {
  117. $this->title = "Error message" ;
  118. $this->errMsg = $errMsg;
  119. $this->render('error',$layout);
  120. }
  121. //Success information page
  122. protected function showSucc($msg,$skipUrl,$skipPage ,$layout = null)
  123. {
  124. $this->title = "Success Tip";
  125. $this->msg = $msg;
  126. $this->skipUrl = $skipUrl;
  127. $this->skipPage = $skipPage;
  128. $this->render('success',$layout);
  129. }
  130. //Show permissioned links
  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,'',$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. }
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn