Controller.class.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. // +----------------------------------------------------------------------
  8. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  9. // +----------------------------------------------------------------------
  10. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  11. // +----------------------------------------------------------------------
  12. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  13. // +----------------------------------------------------------------------
  14. // | Author: liu21st <liu21st@gmail.com>
  15. // +----------------------------------------------------------------------
  16. namespace Think;
  17. /**
  18. * ThinkPHP API模式控制器基类
  19. */
  20. abstract class Controller {
  21. /**
  22. * 架构函数
  23. * @access public
  24. */
  25. public function __construct() {
  26. //控制器初始化
  27. if(method_exists($this,'_initialize'))
  28. $this->_initialize();
  29. }
  30. /**
  31. * 魔术方法 有不存在的操作的时候执行
  32. * @access public
  33. * @param string $method 方法名
  34. * @param array $args 参数
  35. * @return mixed
  36. */
  37. public function __call($method,$args) {
  38. if( 0 === strcasecmp($method,ACTION_NAME.C('ACTION_SUFFIX'))) {
  39. if(method_exists($this,'_empty')) {
  40. // 如果定义了_empty操作 则调用
  41. $this->_empty($method,$args);
  42. }else{
  43. E(L('_ERROR_ACTION_').':'.ACTION_NAME);
  44. }
  45. }else{
  46. E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
  47. return;
  48. }
  49. }
  50. /**
  51. * Ajax方式返回数据到客户端
  52. * @access protected
  53. * @param mixed $data 要返回的数据
  54. * @param String $type AJAX返回数据格式
  55. * @return void
  56. */
  57. protected function ajaxReturn($data,$type='') {
  58. if(empty($type)) $type = C('DEFAULT_AJAX_RETURN');
  59. switch (strtoupper($type)){
  60. case 'JSON' :
  61. // 返回JSON数据格式到客户端 包含状态信息
  62. header('Content-Type:application/json; charset=utf-8');
  63. exit(json_encode($data));
  64. case 'XML' :
  65. // 返回xml格式数据
  66. header('Content-Type:text/xml; charset=utf-8');
  67. exit(xml_encode($data));
  68. case 'JSONP':
  69. // 返回JSON数据格式到客户端 包含状态信息
  70. header('Content-Type:application/json; charset=utf-8');
  71. $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
  72. exit($handler.'('.json_encode($data).');');
  73. case 'EVAL' :
  74. // 返回可执行的js脚本
  75. header('Content-Type:text/html; charset=utf-8');
  76. exit($data);
  77. }
  78. }
  79. /**
  80. * Action跳转(URL重定向) 支持指定模块和延时跳转
  81. * @access protected
  82. * @param string $url 跳转的URL表达式
  83. * @param array $params 其它URL参数
  84. * @param integer $delay 延时跳转的时间 单位为秒
  85. * @param string $msg 跳转提示信息
  86. * @return void
  87. */
  88. protected function redirect($url,$params=array(),$delay=0,$msg='') {
  89. $url = U($url,$params);
  90. redirect($url,$delay,$msg);
  91. }
  92. }