/** * 獲取當前URL 不含QUERY_STRING * @access public * @param string $url URL地址 * @return string */ public function baseUrl($url = null) { if (!is_null($url) && true !== $url) { $this->baseUrl = $url; return $this; } elseif (!$this->baseUrl) { $str = $this->url(); $this->baseUrl = strpos($str, '?') ? strstr($str, '?', true) : $str; } return true === $url ? $this->domain() . $this->baseUrl : $this->baseUrl; }// 獲取基礎 地址 /** * 獲取當前執行的文件 SCRIPT_NAME * @access public * @param string $file 當前執行的文件 * @return string */ public function baseFile($file = null) { if (!is_null($file) && true !== $file) {// 設置 $this->baseFile = $file; return $this; } elseif (!$this->baseFile) {// 獲取 $url = ''; if (!IS_CLI) {// 非命令行 $script_name = basename($_SERVER['SCRIPT_FILENAME']); if (basename($_SERVER['SCRIPT_NAME']) === $script_name) { $url = $_SERVER['SCRIPT_NAME']; } elseif (basename($_SERVER['PHP_SELF']) === $script_name) { $url = $_SERVER['PHP_SELF']; } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $script_name) { $url = $_SERVER['ORIG_SCRIPT_NAME']; } elseif (($pos = strpos($_SERVER['PHP_SELF'], '/' . $script_name)) !== false) { $url = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $script_name; } elseif (isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) === 0) { $url = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME'])); } } $this->baseFile = $url; } return true === $file ? $this->domain() . $this->baseFile : $this->baseFile; }// 獲取基準 文件 /** * 獲取URL訪問根地址 * @access public * @param string $url URL地址 * @return string */ public function root($url = null) {// 獲取 URL 訪問根地址 if (!is_null($url) && true !== $url) { $this->root = $url; return $this; } elseif (!$this->root) { $file = $this->baseFile();// 首先獲取 基礎文件 if ($file && 0 !== strpos($this->url(), $file)) {// 並且這個 什麼 url 跟 這個 $file = str_replace('\\', '/', dirname($file));// 替換路徑 } $this->root = rtrim($file, '/');// 去掉最後一個斜槓 } return true === $url ? $this->domain() . $this->root : $this->root;// 組合出售 } /** * 獲取當前請求URL的pathinfo信息(含URL後綴) * @access public * @return string */ public function pathinfo() { if (is_null($this->pathinfo)) {// 如果路徑信息爲空 if (isset($_GET[Config::get('var_pathinfo')])) { // 判斷URL裏面是否有兼容模式參數 $_SERVER['PATH_INFO'] = $_GET[Config::get('var_pathinfo')]; unset($_GET[Config::get('var_pathinfo')]);// 獲取完成 刪除配置文件 } elseif (IS_CLI) { // CLI模式下 index.php module/controller/action/params/... $_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : ''; }// 寫法居然是這樣的,安逸 index.php module/controller/action/params // 分析PATHINFO信息 if (!isset($_SERVER['PATH_INFO'])) {// 分析 PATHINFO 信息 foreach (Config::get('pathinfo_fetch') as $type) { // 遍歷獲取項目 if (!empty($_SERVER[$type])) {// 如果不爲空 $_SERVER['PATH_INFO'] = (0 === strpos($_SERVER[$type], $_SERVER['SCRIPT_NAME'])) ? substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type]; break; } } } $this->pathinfo = empty($_SERVER['PATH_INFO']) ? '/' : ltrim($_SERVER['PATH_INFO'], '/'); } return $this->pathinfo; }// 獲取完成 /** * 獲取當前請求URL的pathinfo信息(不含URL後綴) * @access public * @return string */ public function path() {// 獲取路徑信息 if (is_null($this->path)) { $suffix = Config::get('url_html_suffix');// 首先獲取 默認設置的後綴 $pathinfo = $this->pathinfo();// 獲取 路徑信息 if (false === $suffix) {// 禁止僞靜態 // 禁止僞靜態訪問 $this->path = $pathinfo; } elseif ($suffix) {// 去除正常的 URL 後綴 // 去除正常的URL後綴 $this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo); } else { // 允許任何後綴訪問 $this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo); } } return $this->path; } /** * 當前URL的訪問後綴 * @access public * @return string */ public function ext()// 當前訪問 URL 後綴 { return pathinfo($this->pathinfo(), PATHINFO_EXTENSION); } /** * 獲取當前請求的時間 * @access public * @param bool $float 是否使用浮點類型 * @return integer|float */ public function time($float = false) { return $float ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME']; }// 獲取當前 請求的 系統時間 /** * 當前請求的資源類型 * @access public * @return false|string */ public function type()// 請求資源類型 { $accept = isset($this->server['HTTP_ACCEPT']) ? $this->server['HTTP_ACCEPT'] : $_SERVER['HTTP_ACCEPT']; // 獲取 accept 數據 if (empty($accept)) { return false; } foreach ($this->mimeType as $key => $val) {// 遍歷循環 $array = explode(',', $val);//分開 foreach ($array as $k => $v) {// 遍歷 if (stristr($accept, $v)) { return $key; } } } return false; } /** * 設置資源類型 * @access public * @param string|array $type 資源類型名 * @param string $val 資源類型 * @return void */ public function mimeType($type, $val = '')// 設置資源類型 { if (is_array($type)) {// 如果是數組 $this->mimeType = array_merge($this->mimeType, $type);// 合併數據類型 } else { $this->mimeType[$type] = $val; } } /** * 當前的請求類型 * @access public * @param bool $method true 獲取原始請求類型 * @return string */ public function method($method = false)// 獲取當前的請求類型 { if (true === $method) {// 獲取原始 請求類型 ? // 獲取原始請求類型 return IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']); } elseif (!$this->method) {// 如果 沒有對應的請求類型 if (isset($_POST[Config::get('var_method')])) {// 提交 裏面設置 請求類型 $this->method = strtoupper($_POST[Config::get('var_method')]); $this->{$this->method}($_POST); } elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {// $this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']); } else {// 默認的 這個情況是有的 $this->method = IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']); } } return $this->method; } /** * 是否爲GET請求 * @access public * @return bool */ public function isGet() { return $this->method() == 'GET'; } /** * 是否爲POST請求 * @access public * @return bool */ public function isPost() { return $this->method() == 'POST'; } /** * 是否爲PUT請求 * @access public * @return bool */ public function isPut() { return $this->method() == 'PUT'; } /** * 是否爲DELTE請求 * @access public * @return bool */ public function isDelete() { return $this->method() == 'DELETE'; } /** * 是否爲HEAD請求 * @access public * @return bool */ public function isHead() { return $this->method() == 'HEAD'; } /** * 是否爲PATCH請求 * @access public * @return bool */ public function isPatch() { return $this->method() == 'PATCH'; } /** * 是否爲OPTIONS請求 * @access public * @return bool */ public function isOptions() { return $this->method() == 'OPTIONS'; } /** * 是否爲cli * @access public * @return bool */ public function isCli() { return PHP_SAPI == 'cli'; } /** * 是否爲cgi * @access public * @return bool */ public function isCgi() { return strpos(PHP_SAPI, 'cgi') === 0; }
[李景山php]每天TP5-20170131|thinkphp5-Request.php-3
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.