Controller.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 API模式控制器基类
  14. */
  15. abstract class Controller
  16. {
  17. /**
  18. * 架构函数
  19. * @access public
  20. */
  21. public function __construct()
  22. {
  23. //控制器初始化
  24. if (method_exists($this, '_initialize')) {
  25. $this->_initialize();
  26. }
  27. }
  28. /**
  29. * 魔术方法 有不存在的操作的时候执行
  30. * @access public
  31. * @param string $method 方法名
  32. * @param array $args 参数
  33. * @return mixed
  34. */
  35. public function __call($method, $args)
  36. {
  37. if (0 === strcasecmp($method, ACTION_NAME . C('ACTION_SUFFIX'))) {
  38. if (method_exists($this, '_empty')) {
  39. // 如果定义了_empty操作 则调用
  40. $this->_empty($method, $args);
  41. } else {
  42. E(L('_ERROR_ACTION_') . ':' . ACTION_NAME);
  43. }
  44. } else {
  45. E(__CLASS__ . ':' . $method . L('_METHOD_NOT_EXIST_'));
  46. return;
  47. }
  48. }
  49. /**
  50. * Ajax方式返回数据到客户端
  51. * @access protected
  52. * @param mixed $data 要返回的数据
  53. * @param String $type AJAX返回数据格式
  54. * @return void
  55. */
  56. protected function ajaxReturn($data, $type = '')
  57. {
  58. if (empty($type)) {
  59. $type = C('DEFAULT_AJAX_RETURN');
  60. }
  61. switch (strtoupper($type)) {
  62. case 'JSON':
  63. // 返回JSON数据格式到客户端 包含状态信息
  64. header('Content-Type:application/json; charset=utf-8');
  65. exit(json_encode($data));
  66. case 'XML':
  67. // 返回xml格式数据
  68. header('Content-Type:text/xml; charset=utf-8');
  69. exit(xml_encode($data));
  70. case 'JSONP':
  71. // 返回JSON数据格式到客户端 包含状态信息
  72. header('Content-Type:application/json; charset=utf-8');
  73. $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
  74. exit($handler . '(' . json_encode($data) . ');');
  75. case 'EVAL':
  76. // 返回可执行的js脚本
  77. header('Content-Type:text/html; charset=utf-8');
  78. exit($data);
  79. }
  80. }
  81. /**
  82. * Action跳转(URL重定向) 支持指定模块和延时跳转
  83. * @access protected
  84. * @param string $url 跳转的URL表达式
  85. * @param array $params 其它URL参数
  86. * @param integer $delay 延时跳转的时间 单位为秒
  87. * @param string $msg 跳转提示信息
  88. * @return void
  89. */
  90. protected function redirect($url, $params = array(), $delay = 0, $msg = '')
  91. {
  92. $url = U($url, $params);
  93. redirect($url, $delay, $msg);
  94. }
  95. }