Controller.class.php 9.9 KB

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