Controller.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace KIF\Core;
  3. /**
  4. * 定义MVC里的C抽象类,即Controller
  5. * @author gaoxiaogang@gmail.com
  6. */
  7. use KIF\String\Filter;
  8. use KIF\Core\View;
  9. use Exception;
  10. abstract class Controller {
  11. protected $action;
  12. /**
  13. *
  14. * 所有输出到模版的变量,都存放到这里面,以便统一管理
  15. * @var array
  16. */
  17. protected $output = array();
  18. protected $tpl;
  19. /**
  20. * 导航菜单
  21. *
  22. * @var Array
  23. */
  24. protected $navMenus = array();
  25. /**
  26. *
  27. * 控制器入口方法
  28. *
  29. */
  30. abstract public function run();
  31. /**
  32. *
  33. * 设定指定的名、值,模版里可以使用到
  34. * @param string $name
  35. * @param mixed $value
  36. * @throws Exception
  37. */
  38. protected function setOutput($name, $value) {
  39. if (!is_string($name)) {
  40. throw new Exception('set output to template error, name not string !');
  41. }
  42. $this->output[$name] = $value;
  43. }
  44. /**
  45. *
  46. * 设置行为
  47. * @param string $action
  48. */
  49. public function setAction($action) {
  50. if (empty($action)) {
  51. $action = 'default';
  52. }
  53. $this->action = 'do' . ucfirst($action);
  54. }
  55. /**
  56. * 添加导航栏
  57. *
  58. * @param string $title
  59. * @param string $href
  60. * @param string $target
  61. * @param string $icon
  62. * @return \KIF\Core\Controller
  63. */
  64. protected function addNavMenu($title, $href = null, $target = '_self', $icon = null) {
  65. $this->navMenus[] = array(
  66. 'title' => $title,
  67. 'href' => $href,
  68. 'target' => $target,
  69. 'icon' => $icon,
  70. );
  71. return $this;
  72. }
  73. /**
  74. *
  75. * 设置一批输出
  76. * @param array $outputs
  77. *
  78. */
  79. protected function setOutputs(array $outputs) {
  80. $this->output = array_merge($this->output, $outputs);
  81. }
  82. /**
  83. *
  84. * 渲染结果
  85. * @param boolean $return 是否返回。true:返回渲染结果;false:直接输出结果
  86. * @return string | NULL
  87. */
  88. public function render($return = false) {
  89. if (!$this->tpl) {// 没有指定模文件
  90. return null;
  91. }
  92. $objView = new View();
  93. $this->output['navMenus'] = $this->navMenus;
  94. $config = Config::getInstance()->current();
  95. $this->output['web_cfg'] = $config['web_cfg'];
  96. $objView->assign($this->output);
  97. if ($return) {
  98. return $objView->r($this->tpl);
  99. } else {
  100. $objView->d($this->tpl);
  101. }
  102. }
  103. /**
  104. *
  105. * 操作失败
  106. * !!如果页面请求的参数里含有 cross_cb,则认为是需要做跨域ajax的支持,跳转到 cross_cb 参数里指定的url
  107. * @param string $msg 失败描述
  108. */
  109. protected function ajax_fail_exit($msg) {
  110. $return = array(
  111. 'ok' => false,
  112. 'msg' => $msg,
  113. );
  114. # 支持跨域的AJAX GET,其实是基于jQuery的$.getJson实现的。之所以把AJAX POST、GET做不同实现,是出于性能和可用性考虑。
  115. $jsonp_cb = Request::r('jsonp_cb', Filter::TRIM);
  116. if ($jsonp_cb) {
  117. $jsonp_cb = Filter::htmlspecialchars($jsonp_cb);// 防止css漏洞
  118. $this->echo_exit("{$jsonp_cb}(".json_encode($return).")");
  119. }
  120. $this->echo_exit(json_encode($return));
  121. }
  122. /**
  123. *
  124. * 操作成功
  125. * !!如果页面请求的参数里含有 cross_cb,则认为是需要做跨域ajax的支持,跳转到 cross_cb 参数里指定的url
  126. * @param string $msg 成功描述
  127. */
  128. protected function ajax_success_exit($msg) {
  129. $return = array(
  130. 'ok' => true,
  131. 'msg' => $msg,
  132. );
  133. # 支持跨域的AJAX GET,其实是基于jQuery的$.getJson实现的。之所以把AJAX POST、GET做不同实现,是出于性能和可用性考虑。
  134. $jsonp_cb = Request::r('jsonp_cb', Filter::TRIM);
  135. if ($jsonp_cb) {
  136. $jsonp_cb = Filter::htmlspecialchars($jsonp_cb);// 防止css漏洞
  137. $this->echo_exit("{$jsonp_cb}(".json_encode($return).")");
  138. }
  139. $this->echo_exit(json_encode($return));
  140. }
  141. protected function echo_exit($msg) {
  142. echo $msg;
  143. exit;
  144. }
  145. protected function echo_msg($msg) {
  146. echo '['.date('Y-m-d H:i:s').'] '. $msg;
  147. $this->newline();
  148. }
  149. /**
  150. *
  151. * 输出新行。
  152. */
  153. protected function newline() {
  154. if (Request::isCLI()) {
  155. echo "\r\n";
  156. } else {
  157. echo "<br />";
  158. }
  159. }
  160. /**
  161. * 链接跳转
  162. * @param string $url 跳转的url
  163. * @return void
  164. **/
  165. protected function redirect($url,$status ='') {
  166. if ($status == '301') {
  167. header("HTTP/1.1 301 Moved Permanently");
  168. }
  169. if (!empty($url)) {
  170. header("Location: ".$url."");
  171. }
  172. exit;
  173. }
  174. /**
  175. * 输出错误消息 - 后台
  176. * @author li.shuming@kimiss.com
  177. * @param string $msg
  178. */
  179. protected function fail_exit_bs($msg = null) {
  180. $permission_template_dir = Config::getInstance()->get('App_Path') . DS . 'template_dir';
  181. $this->tpl = $permission_template_dir . '/prompt_message';
  182. $this->setOutputs(array(
  183. 'type' => 'fail',
  184. 'msg' => $msg,
  185. 'referer' => Request::referer(),
  186. 'header_tpl'=> $permission_template_dir . '/header.html',
  187. 'bottom_tpl'=> $permission_template_dir . '/bottom.html',
  188. ));
  189. $this->render();
  190. exit;
  191. }
  192. /**
  193. * 输出成功消息 - 后台
  194. * @author li.shuming@kimiss.com
  195. * @param string $msg
  196. */
  197. protected function success_exit_bs($msg = null) {
  198. $permission_template_dir = Config::getInstance()->get('App_Path') . DS . 'template_dir';
  199. $this->tpl = $permission_template_dir . '/prompt_message';
  200. $this->setOutputs(array(
  201. 'type' => 'success',
  202. 'msg' => $msg,
  203. 'referer' => Request::referer(),
  204. 'header_tpl'=> $permission_template_dir . '/header.html',
  205. 'bottom_tpl'=> $permission_template_dir . '/bottom.html',
  206. ));
  207. $this->render();
  208. exit;
  209. }
  210. }