PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/xianpipa/ThinkPHP/Library/Think/Controller.class.php

https://gitlab.com/fangfangchen/xianpipa
PHP | 307 lines | 123 code | 20 blank | 164 comment | 15 complexity | c8c7cae3493be892ca7e9ee3691d961d MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think;
  12. /**
  13. * ThinkPHP 控制器基类 抽象类
  14. */
  15. abstract class Controller {
  16. /**
  17. * 视图实例对象
  18. * @var view
  19. * @access protected
  20. */
  21. protected $view = null;
  22. /**
  23. * 控制器参数
  24. * @var config
  25. * @access protected
  26. */
  27. protected $config = array();
  28. /**
  29. * 架构函数 取得模板对象实例
  30. * @access public
  31. */
  32. public function __construct() {
  33. Hook::listen('action_begin',$this->config);
  34. //实例化视图类
  35. $this->view = Think::instance('Think\View');
  36. //控制器初始化
  37. if(method_exists($this,'_initialize'))
  38. $this->_initialize();
  39. }
  40. /**
  41. * 模板显示 调用内置的模板引擎显示方法,
  42. * @access protected
  43. * @param string $templateFile 指定要调用的模板文件
  44. * 默认为空 由系统自动定位模板文件
  45. * @param string $charset 输出编码
  46. * @param string $contentType 输出类型
  47. * @param string $content 输出内容
  48. * @param string $prefix 模板缓存前缀
  49. * @return void
  50. */
  51. protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
  52. $this->view->display($templateFile,$charset,$contentType,$content,$prefix);
  53. }
  54. /**
  55. * 输出内容文本可以包括Html 并支持内容解析
  56. * @access protected
  57. * @param string $content 输出内容
  58. * @param string $charset 模板输出字符集
  59. * @param string $contentType 输出类型
  60. * @param string $prefix 模板缓存前缀
  61. * @return mixed
  62. */
  63. protected function show($content,$charset='',$contentType='',$prefix='') {
  64. $this->view->display('',$charset,$contentType,$content,$prefix);
  65. }
  66. /**
  67. * 获取输出页面内容
  68. * 调用内置的模板引擎fetch方法,
  69. * @access protected
  70. * @param string $templateFile 指定要调用的模板文件
  71. * 默认为空 由系统自动定位模板文件
  72. * @param string $content 模板输出内容
  73. * @param string $prefix 模板缓存前缀*
  74. * @return string
  75. */
  76. protected function fetch($templateFile='',$content='',$prefix='') {
  77. return $this->view->fetch($templateFile,$content,$prefix);
  78. }
  79. /**
  80. * 创建静态页面
  81. * @access protected
  82. * @htmlfile 生成的静态文件名称
  83. * @htmlpath 生成的静态文件路径
  84. * @param string $templateFile 指定要调用的模板文件
  85. * 默认为空 由系统自动定位模板文件
  86. * @return string
  87. */
  88. protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='') {
  89. $content = $this->fetch($templateFile);
  90. $htmlpath = !empty($htmlpath)?$htmlpath:HTML_PATH;
  91. $htmlfile = $htmlpath.$htmlfile.C('HTML_FILE_SUFFIX');
  92. Storage::put($htmlfile,$content,'html');
  93. return $content;
  94. }
  95. /**
  96. * 模板主题设置
  97. * @access protected
  98. * @param string $theme 模版主题
  99. * @return Action
  100. */
  101. protected function theme($theme){
  102. $this->view->theme($theme);
  103. return $this;
  104. }
  105. /**
  106. * 模板变量赋值
  107. * @access protected
  108. * @param mixed $name 要显示的模板变量
  109. * @param mixed $value 变量的值
  110. * @return Action
  111. */
  112. protected function assign($name,$value='') {
  113. $this->view->assign($name,$value);
  114. return $this;
  115. }
  116. public function __set($name,$value) {
  117. $this->assign($name,$value);
  118. }
  119. /**
  120. * 取得模板显示变量的值
  121. * @access protected
  122. * @param string $name 模板显示变量
  123. * @return mixed
  124. */
  125. public function get($name='') {
  126. return $this->view->get($name);
  127. }
  128. public function __get($name) {
  129. return $this->get($name);
  130. }
  131. /**
  132. * 检测模板变量的值
  133. * @access public
  134. * @param string $name 名称
  135. * @return boolean
  136. */
  137. public function __isset($name) {
  138. return $this->get($name);
  139. }
  140. /**
  141. * 魔术方法 有不存在的操作的时候执行
  142. * @access public
  143. * @param string $method 方法名
  144. * @param array $args 参数
  145. * @return mixed
  146. */
  147. public function __call($method,$args) {
  148. if( 0 === strcasecmp($method,ACTION_NAME.C('ACTION_SUFFIX'))) {
  149. if(method_exists($this,'_empty')) {
  150. // 如果定义了_empty操作 则调用
  151. $this->_empty($method,$args);
  152. }elseif(file_exists_case($this->view->parseTemplate())){
  153. // 检查是否存在默认模版 如果有直接输出模版
  154. $this->display();
  155. }else{
  156. E(L('_ERROR_ACTION_').':'.ACTION_NAME);
  157. }
  158. }else{
  159. E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
  160. return;
  161. }
  162. }
  163. /**
  164. * 操作错误跳转的快捷方法
  165. * @access protected
  166. * @param string $message 错误信息
  167. * @param string $jumpUrl 页面跳转地址
  168. * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
  169. * @return void
  170. */
  171. protected function error($message='',$jumpUrl='',$ajax=false) {
  172. $this->dispatchJump($message,0,$jumpUrl,$ajax);
  173. }
  174. /**
  175. * 操作成功跳转的快捷方法
  176. * @access protected
  177. * @param string $message 提示信息
  178. * @param string $jumpUrl 页面跳转地址
  179. * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
  180. * @return void
  181. */
  182. protected function success($message='',$jumpUrl='',$ajax=false) {
  183. $this->dispatchJump($message,1,$jumpUrl,$ajax);
  184. }
  185. /**
  186. * Ajax方式返回数据到客户端
  187. * @access protected
  188. * @param mixed $data 要返回的数据
  189. * @param String $type AJAX返回数据格式
  190. * @param int $json_option 传递给json_encode的option参数
  191. * @return void
  192. */
  193. protected function ajaxReturn($data,$type='',$json_option=0) {
  194. if(empty($type)) $type = C('DEFAULT_AJAX_RETURN');
  195. switch (strtoupper($type)){
  196. case 'JSON' :
  197. // 返回JSON数据格式到客户端 包含状态信息
  198. header('Content-Type:application/json; charset=utf-8');
  199. exit(json_encode($data,$json_option));
  200. case 'XML' :
  201. // 返回xml格式数据
  202. header('Content-Type:text/xml; charset=utf-8');
  203. exit(xml_encode($data));
  204. case 'JSONP':
  205. // 返回JSON数据格式到客户端 包含状态信息
  206. header('Content-Type:application/json; charset=utf-8');
  207. $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
  208. exit($handler.'('.json_encode($data,$json_option).');');
  209. case 'EVAL' :
  210. // 返回可执行的js脚本
  211. header('Content-Type:text/html; charset=utf-8');
  212. exit($data);
  213. default :
  214. // 用于扩展其他返回格式数据
  215. Hook::listen('ajax_return',$data);
  216. }
  217. }
  218. /**
  219. * Action跳转(URL重定向) 支持指定模块和延时跳转
  220. * @access protected
  221. * @param string $url 跳转的URL表达式
  222. * @param array $params 其它URL参数
  223. * @param integer $delay 延时跳转的时间 单位为秒
  224. * @param string $msg 跳转提示信息
  225. * @return void
  226. */
  227. protected function redirect($url,$params=array(),$delay=0,$msg='') {
  228. $url = U($url,$params);
  229. redirect($url,$delay,$msg);
  230. }
  231. /**
  232. * 默认跳转操作 支持错误导向和正确跳转
  233. * 调用模板显示 默认为public目录下面的success页面
  234. * 提示页面为可配置 支持模板标签
  235. * @param string $message 提示信息
  236. * @param Boolean $status 状态
  237. * @param string $jumpUrl 页面跳转地址
  238. * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
  239. * @access private
  240. * @return void
  241. */
  242. private function dispatchJump($message,$status=1,$jumpUrl='',$ajax=false) {
  243. if(true === $ajax || IS_AJAX) {// AJAX提交
  244. $data = is_array($ajax)?$ajax:array();
  245. $data['info'] = $message;
  246. $data['status'] = $status;
  247. $data['url'] = $jumpUrl;
  248. $this->ajaxReturn($data);
  249. }
  250. if(is_int($ajax)) $this->assign('waitSecond',$ajax);
  251. if(!empty($jumpUrl)) $this->assign('jumpUrl',$jumpUrl);
  252. // 提示标题
  253. $this->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
  254. //如果设置了关闭窗口,则提示完毕后自动关闭窗口
  255. if($this->get('closeWin')) $this->assign('jumpUrl','javascript:window.close();');
  256. $this->assign('status',$status); // 状态
  257. //保证输出不受静态缓存影响
  258. C('HTML_CACHE_ON',false);
  259. if($status) { //发送成功信息
  260. $this->assign('message',$message);// 提示信息
  261. // 成功操作后默认停留1秒
  262. if(!isset($this->waitSecond)) $this->assign('waitSecond','1');
  263. // 默认操作成功自动返回操作前页面
  264. if(!isset($this->jumpUrl)) $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
  265. $this->display(C('TMPL_ACTION_SUCCESS'));
  266. }else{
  267. $this->assign('error',$message);// 提示信息
  268. //发生错误时候默认停留3秒
  269. if(!isset($this->waitSecond)) $this->assign('waitSecond','3');
  270. // 默认发生错误的话自动返回上页
  271. if(!isset($this->jumpUrl)) $this->assign('jumpUrl',"javascript:history.back(-1);");
  272. $this->display(C('TMPL_ACTION_ERROR'));
  273. // 中止执行 避免出错后继续执行
  274. exit ;
  275. }
  276. }
  277. /**
  278. * 析构方法
  279. * @access public
  280. */
  281. public function __destruct() {
  282. // 执行后续操作
  283. Hook::listen('action_end');
  284. }
  285. }
  286. // 设置控制器别名 便于升级
  287. class_alias('Think\Controller','Think\Action');