markdown格式wiki文档

Request.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app;
  3. use app\utils\Str;
  4. class Request
  5. {
  6. /**
  7. * 请求方法是否为Post
  8. * @var bool
  9. */
  10. protected $isPost = false;
  11. /**
  12. * 控制器名
  13. * @var string
  14. */
  15. public $controller;
  16. /**
  17. * 操作名
  18. * @var string
  19. */
  20. public $actionName;
  21. /**
  22. * 参数
  23. * @var array
  24. */
  25. public $params;
  26. /**
  27. * 初始化
  28. * @var bool
  29. */
  30. protected static $initialized = false;
  31. //创建静态私有的变量保存该类对象
  32. private static $instance;
  33. //防止直接创建对象
  34. private function __construct(){
  35. }
  36. public static function getInstance($initforce = false){
  37. //判断$instance是否是APP的对象 没有则创建
  38. if (!self::$instance instanceof self) {
  39. self::$instance = new self();
  40. }
  41. // 判断是否强制初始化及是否未初始化
  42. if ($initforce || !self::$initialized) {
  43. self::$instance->init();
  44. }
  45. return self::$instance;
  46. }
  47. public function init()
  48. {
  49. self::$initialized = true;
  50. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  51. $this->isPost = true;
  52. }
  53. $pathinfo = "";
  54. if (isset($_SERVER['PATH_INFO']) && !empty($_SERVER['PATH_INFO']) ) {
  55. $pathinfo = $_SERVER['PATH_INFO'];
  56. } else {
  57. $pathinfo = strpos($_SERVER['REQUEST_URI'], '?') ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'];
  58. }
  59. if (is_string($pathinfo)) {
  60. $result = explode('/', trim($pathinfo,'/'));
  61. }
  62. // 获取控制器名
  63. // $controller = strip_tags($result[0] ?: 'Index');
  64. // $this->controller = camelize($controller);
  65. // 获取操作名
  66. // $this->actionName = strip_tags(isset($result[1]) ? $result[1] : 'index');
  67. // 控制器名只用 Index
  68. $this->controller = "Index";
  69. $this->actionName = strip_tags(isset($result[0]) && !empty($result[0]) ? $result[0] : 'index');
  70. // 文档路径
  71. $doc = '';
  72. if (count($result) > 1)
  73. {
  74. for ($i = 1; $i < count($result); $i++) {
  75. $doc .= '/' . $result[$i];
  76. }
  77. }
  78. $this->params = array_merge($_GET, $_POST, ['doc'=>$doc]);
  79. return $this;
  80. }
  81. /**
  82. * 是否初始化过
  83. * @return bool
  84. */
  85. public function initialized()
  86. {
  87. return $this->initialized;
  88. }
  89. public function isPost()
  90. {
  91. return $this->isPost;
  92. }
  93. }