Controller.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace PhpLife\Frame;
  3. abstract class Controller
  4. {
  5. protected $tpl; // 模板路径, 可选
  6. protected $queryObj;
  7. final public function __construct()
  8. {
  9. $this->queryObj = $this->getQuery();
  10. }
  11. /**
  12. * 主逻辑
  13. */
  14. final public function run()
  15. {
  16. try {
  17. $this->before();
  18. $this->checkAuth();
  19. $this->main();
  20. } catch (\Exception $ex) {
  21. $this->showError($ex->getCode(), $ex->getMessage());
  22. }
  23. }
  24. /**
  25. * 尽快输出结果,然后执行后续逻辑
  26. * 本方法的的信息不会被输出
  27. * 即使exit之后仍旧能被执行
  28. */
  29. final public function __destruct()
  30. {
  31. if (function_exists('fastcgi_finish_request')) {
  32. fastcgi_finish_request();
  33. }
  34. $this->after();
  35. }
  36. /**
  37. * 前置方法,处理参数
  38. */
  39. protected function before()
  40. {
  41. }
  42. /**
  43. * 执行验证
  44. */
  45. protected function checkAuth()
  46. {
  47. }
  48. /**
  49. * 执行逻辑
  50. * @return mixed
  51. */
  52. protected abstract function main();
  53. /**
  54. * 后续方法
  55. */
  56. protected function after()
  57. {
  58. }
  59. /**
  60. * 显示错误信息
  61. * @param int $code
  62. * @param string $message
  63. */
  64. protected function showError($code = 0, $message = '')
  65. {
  66. echo "[$code] $message \n<br>";
  67. exit;
  68. }
  69. /**
  70. * @return Filter
  71. */
  72. protected function getRequest()
  73. {
  74. static $instance = null;
  75. if (is_null($instance)) {
  76. $instance = new Filter($_REQUEST);
  77. }
  78. return $instance;
  79. }
  80. /**
  81. * @return Filter
  82. */
  83. protected function getQuery()
  84. {
  85. static $instance = null;
  86. if (is_null($instance)) {
  87. $instance = new Filter($_GET);
  88. }
  89. return $instance;
  90. }
  91. /**
  92. * @return Filter
  93. */
  94. protected function getPost()
  95. {
  96. static $instance = null;
  97. if (is_null($instance)) {
  98. $instance = new Filter($_POST);
  99. }
  100. return $instance;
  101. }
  102. /**
  103. * 获取模板, 固定模板, 与controller名称类似
  104. * @return \PhpLife\Frame\Template
  105. */
  106. protected function getTemplate()
  107. {
  108. static $template;
  109. if ($template) {
  110. return $template;
  111. }
  112. $class = get_called_class();
  113. $class = explode('\\', $class);
  114. $path = dirname(__DIR__) . '/' . implode('/', array_slice($class, 1, 2)) . '/Template/';
  115. if(!$this->tpl){
  116. $this->tpl = implode('/', array_slice($class, 4));
  117. }
  118. $template = new Template();
  119. $template->setPath($path)->setTpl($this->tpl);
  120. $data = get_object_vars($this);
  121. if ($data) {
  122. $template->assign($data);
  123. }
  124. return $template;
  125. }
  126. /**
  127. * 检查CSRF攻击
  128. * @return bool
  129. */
  130. protected function checkCsrf()
  131. {
  132. $host = strtolower(trim($_SERVER['HTTP_HOST']));
  133. $referer = parse_url($_SERVER['HTTP_REFERER']);
  134. $referer = substr(strtolower(trim($referer['host'])), -(strlen($host)));
  135. if (!empty($referer) AND $referer != $host AND $referer != ('www.' . $host)) {
  136. return false;
  137. }
  138. return true;
  139. }
  140. }