[李景山php]每天TP5-20170131|thinkphp5-Request.php-3

來源:互聯網
上載者:User

標籤:thinkphp5

/** * 擷取當前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 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1889152

[李景山php]每天TP5-20170131|thinkphp5-Request.php-3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.