Controller.class.php 11 KB

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