123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- namespace think;
- use think\exception\HttpResponseException;
- class Request
- {
- /**
- * 对象实例
- * @var object
- */
- protected $instance;
- /**
- * 配置对象
- * @var Config
- */
- protected $config;
- /**
- * 请求类型
- * @var string
- */
- protected $method;
- /**
- * 域名(含协议及端口)
- * @var string
- */
- protected $domain;
- /**
- * 子域名
- * @var string
- */
- protected $subDomain;
- /**
- * 泛域名
- * @var string
- */
- protected $panDomain;
- /**
- * 当前URL地址
- * @var string
- */
- protected $url;
- /**
- * 基础URL
- * @var string
- */
- protected $baseUrl;
- /**
- * 当前执行的文件
- * @var string
- */
- protected $baseFile;
- /**
- * 访问的ROOT地址
- * @var string
- */
- protected $root;
- /**
- * pathinfo
- * @var string
- */
- protected $pathinfo;
- /**
- * pathinfo(不含后缀)
- * @var string
- */
- protected $path;
- /**
- * 当前路由信息
- * @var array
- */
- protected $routeInfo = [];
- /**
- * 当前调度信息
- * @var array
- */
- protected $dispatch = [];
- /**
- * 当前模块名
- * @var string
- */
- protected $module;
- /**
- * 当前控制器名
- * @var string
- */
- protected $controller;
- /**
- * 当前操作名
- * @var string
- */
- protected $action;
- /**
- * 当前语言集
- * @var string
- */
- protected $langset;
- /**
- * 当前请求参数
- * @var array
- */
- protected $param = [];
- /**
- * 当前GET参数
- * @var array
- */
- protected $get = [];
- /**
- * 当前POST参数
- * @var array
- */
- protected $post = [];
- /**
- * 当前REQUEST参数
- * @var array
- */
- protected $request = [];
- /**
- * 当前ROUTE参数
- * @var array
- */
- protected $route = [];
- /**
- * 当前PUT参数
- * @var array
- */
- protected $put;
- /**
- * 当前SESSION参数
- * @var array
- */
- protected $session = [];
- /**
- * 当前FILE参数
- * @var array
- */
- protected $file = [];
- /**
- * 当前COOKIE参数
- * @var array
- */
- protected $cookie = [];
- /**
- * 当前SERVER参数
- * @var array
- */
- protected $server = [];
- /**
- * 当前ENV参数
- * @var array
- */
- protected $env = [];
- /**
- * 当前HEADER参数
- * @var array
- */
- protected $header = [];
- /**
- * 资源类型定义
- * @var array
- */
- protected $mimeType = [
- 'xml' => 'application/xml,text/xml,application/x-xml',
- 'json' => 'application/json,text/x-json,application/jsonrequest,text/json',
- 'js' => 'text/javascript,application/javascript,application/x-javascript',
- 'css' => 'text/css',
- 'rss' => 'application/rss+xml',
- 'yaml' => 'application/x-yaml,text/yaml',
- 'atom' => 'application/atom+xml',
- 'pdf' => 'application/pdf',
- 'text' => 'text/plain',
- 'image' => 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*',
- 'csv' => 'text/csv',
- 'html' => 'text/html,application/xhtml+xml,*/*',
- ];
- /**
- * 当前请求内容
- * @var string
- */
- protected $content;
- /**
- * 全局过滤规则
- * @var array
- */
- protected $filter;
- /**
- * 扩展方法
- * @var array
- */
- protected $hook = [];
- /**
- * php://input内容
- * @var string
- */
- // php://input
- protected $input;
- /**
- * 请求缓存
- * @var array
- */
- protected $cache;
- /**
- * 缓存是否检查
- * @var bool
- */
- protected $isCheckCache;
- /**
- * 架构函数
- * @access public
- * @param array $options 参数
- */
- public function __construct($options = [])
- {
- foreach ($options as $name => $item) {
- if (property_exists($this, $name)) {
- $this->$name = $item;
- }
- }
- $this->config = Container::get('config');
- if (is_null($this->filter)) {
- $this->filter = $this->config->get('default_filter');
- }
- // 保存 php://input
- $this->input = file_get_contents('php://input');
- }
- public function __call($method, $args)
- {
- if (array_key_exists($method, $this->hook)) {
- array_unshift($args, $this);
- return call_user_func_array($this->hook[$method], $args);
- } else {
- throw new Exception('method not exists:' . static::class . '->' . $method);
- }
- }
- /**
- * Hook 方法注入
- * @access public
- * @param string|array $method 方法名
- * @param mixed $callback callable
- * @return void
- */
- public function hook($method, $callback = null)
- {
- if (is_array($method)) {
- $this->hook = array_merge($this->hook, $method);
- } else {
- $this->hook[$method] = $callback;
- }
- }
- /**
- * 创建一个URL请求
- * @access public
- * @param string $uri URL地址
- * @param string $method 请求类型
- * @param array $params 请求参数
- * @param array $cookie
- * @param array $files
- * @param array $server
- * @param string $content
- * @return \think\Request
- */
- public function create($uri, $method = 'GET', $params = [], $cookie = [], $files = [], $server = [], $content = null)
- {
- $server['PATH_INFO'] = '';
- $server['REQUEST_METHOD'] = strtoupper($method);
- $info = parse_url($uri);
- if (isset($info['host'])) {
- $server['SERVER_NAME'] = $info['host'];
- $server['HTTP_HOST'] = $info['host'];
- }
- if (isset($info['scheme'])) {
- if ('https' === $info['scheme']) {
- $server['HTTPS'] = 'on';
- $server['SERVER_PORT'] = 443;
- } else {
- unset($server['HTTPS']);
- $server['SERVER_PORT'] = 80;
- }
- }
- if (isset($info['port'])) {
- $server['SERVER_PORT'] = $info['port'];
- $server['HTTP_HOST'] = $server['HTTP_HOST'] . ':' . $info['port'];
- }
- if (isset($info['user'])) {
- $server['PHP_AUTH_USER'] = $info['user'];
- }
- if (isset($info['pass'])) {
- $server['PHP_AUTH_PW'] = $info['pass'];
- }
- if (!isset($info['path'])) {
- $info['path'] = '/';
- }
- $options = [];
- $options[strtolower($method)] = $params;
- $queryString = '';
- if (isset($info['query'])) {
- parse_str(html_entity_decode($info['query']), $query);
- if (!empty($params)) {
- $params = array_replace($query, $params);
- $queryString = http_build_query($query, '', '&');
- } else {
- $params = $query;
- $queryString = $info['query'];
- }
- } elseif (!empty($params)) {
- $queryString = http_build_query($params, '', '&');
- }
- if ($queryString) {
- parse_str($queryString, $get);
- $options['get'] = isset($options['get']) ? array_merge($get, $options['get']) : $get;
- }
- $server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
- $server['QUERY_STRING'] = $queryString;
- $options['cookie'] = $cookie;
- $options['param'] = $params;
- $options['file'] = $files;
- $options['server'] = $server;
- $options['url'] = $server['REQUEST_URI'];
- $options['baseUrl'] = $info['path'];
- $options['pathinfo'] = '/' == $info['path'] ? '/' : ltrim($info['path'], '/');
- $options['method'] = $server['REQUEST_METHOD'];
- $options['domain'] = isset($info['scheme']) ? $info['scheme'] . '://' . $server['HTTP_HOST'] : '';
- $options['content'] = $content;
- foreach ($options as $name => $item) {
- if (property_exists($this, $name)) {
- $this->$name = $item;
- }
- }
- return $this;
- }
- /**
- * 设置或获取当前包含协议的域名
- * @access public
- * @param string $domain 域名
- * @return string|$this
- */
- public function domain($domain = null)
- {
- if (!is_null($domain)) {
- $this->domain = $domain;
- return $this;
- } elseif (!$this->domain) {
- $this->domain = $this->scheme() . '://' . $this->host();
- }
- return $this->domain;
- }
- /**
- * 获取当前子域名
- * @access public
- * @return string|$this
- */
- public function subDomain()
- {
- if (is_null($this->subDomain)) {
- // 获取当前主域名
- $rootDomain = $this->config->get('app.url_domain_root');
- if ($rootDomain) {
- // 配置域名根 例如 thinkphp.cn 163.com.cn 如果是国家级域名 com.cn net.cn 之类的域名需要配置
- $domain = explode('.', rtrim(stristr($this->host(), $rootDomain, true), '.'));
- } else {
- $domain = explode('.', $this->host(), -2);
- }
- $this->subDomain = implode('.', $domain);
- }
- return $this->subDomain;
- }
- /**
- * 设置或获取当前泛域名的值
- * @access public
- * @param string $domain 域名
- * @return string|$this
- */
- public function panDomain($domain = null)
- {
- if (is_null($domain)) {
- return $this->panDomain;
- } else {
- $this->panDomain = $domain;
- return $this;
- }
- }
- /**
- * 设置或获取当前完整URL 包括QUERY_STRING
- * @access public
- * @param string|true $url URL地址 true 带域名获取
- * @return string|$this
- */
- public function url($url = null)
- {
- if (!is_null($url) && true !== $url) {
- $this->url = $url;
- return $this;
- } elseif (!$this->url) {
- if ($this->isCli()) {
- $this->url = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
- } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
- $this->url = $_SERVER['HTTP_X_REWRITE_URL'];
- } elseif (isset($_SERVER['REQUEST_URI'])) {
- $this->url = $_SERVER['REQUEST_URI'];
- } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
- $this->url = $_SERVER['ORIG_PATH_INFO'] . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
- } else {
- $this->url = '';
- }
- }
- return true === $url ? $this->domain() . $this->url : $this->url;
- }
- /**
- * 设置或获取当前URL 不含QUERY_STRING
- * @access public
- * @param string $url URL地址
- * @return string|$this
- */
- 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|$this
- */
- public function baseFile($file = null)
- {
- if (!is_null($file) && true !== $file) {
- $this->baseFile = $file;
- return $this;
- } elseif (!$this->baseFile) {
- $url = '';
- if (!$this->isCli()) {
- $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|$this
- */
- public function root($url = null)
- {
- if (!is_null($url) && true !== $url) {
- $this->root = $url;
- return $this;
- } elseif (!$this->root) {
- $file = $this->baseFile();
- if ($file && 0 !== strpos($this->url(), $file)) {
- $file = str_replace('\\', '/', dirname($file));
- }
- $this->root = rtrim($file, '/');
- }
- return true === $url ? $this->domain() . $this->root : $this->root;
- }
- /**
- * 获取URL访问根目录
- * @access public
- * @return string
- */
- public function rootUrl()
- {
- $base = $this->root();
- $root = strpos($base, '.') ? ltrim(dirname($base), DIRECTORY_SEPARATOR) : $base;
- if ('' != $root) {
- $root = '/' . ltrim($root, '/');
- }
- return $root;
- }
- /**
- * 获取当前请求URL的pathinfo信息(含URL后缀)
- * @access public
- * @return string
- */
- public function pathinfo()
- {
- if (is_null($this->pathinfo)) {
- if (isset($_GET[$this->config->get('var_pathinfo')])) {
- // 判断URL里面是否有兼容模式参数
- $_SERVER['PATH_INFO'] = $_GET[$this->config->get('var_pathinfo')];
- unset($_GET[$this->config->get('var_pathinfo')]);
- } elseif ($this->isCli()) {
- // CLI模式下 index.php module/controller/action/params/...
- $_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
- } elseif ('cli-server' == PHP_SAPI) {
- $_SERVER['PATH_INFO'] = strpos($_SERVER['REQUEST_URI'], '?') ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'];
- }
- // 分析PATHINFO信息
- if (!isset($_SERVER['PATH_INFO'])) {
- foreach ($this->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 = $this->config->get('url_html_suffix');
- $pathinfo = $this->pathinfo();
- if (false === $suffix) {
- // 禁止伪静态访问
- $this->path = $pathinfo;
- } elseif ($suffix) {
- // 去除正常的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()
- {
- 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 = $this->server('HTTP_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 $this->isCli() ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
- } elseif (!$this->method) {
- if (isset($_POST[$this->config->get('var_method')])) {
- $this->method = strtoupper($_POST[$this->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 = $this->isCli() ? '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;
- }
- /**
- * 获取当前请求的参数
- * @access public
- * @param mixed $name 变量名
- * @param mixed $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function param($name = '', $default = null, $filter = '')
- {
- if (empty($this->param)) {
- $method = $this->method(true);
- // 自动获取请求变量
- switch ($method) {
- case 'POST':
- $vars = $this->post(false);
- break;
- case 'PUT':
- case 'DELETE':
- case 'PATCH':
- $vars = $this->put(false);
- break;
- default:
- $vars = [];
- }
- // 当前请求参数和URL地址中的参数合并
- $this->param = array_merge($this->get(false), $vars, $this->route(false));
- }
- if (true === $name) {
- // 获取包含文件上传信息的数组
- $file = $this->file();
- $data = is_array($file) ? array_merge($this->param, $file) : $this->param;
- return $this->input($data, '', $default, $filter);
- }
- return $this->input($this->param, $name, $default, $filter);
- }
- /**
- * 设置获取路由参数
- * @access public
- * @param mixed $name 变量名
- * @param mixed $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function route($name = '', $default = null, $filter = '')
- {
- if (is_array($name)) {
- $this->param = [];
- return $this->route = array_merge($this->route, $name);
- }
- return $this->input($this->route, $name, $default, $filter);
- }
- /**
- * 设置获取GET参数
- * @access public
- * @param mixed $name 变量名
- * @param mixed $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function get($name = '', $default = null, $filter = '')
- {
- if (empty($this->get)) {
- $this->get = $_GET;
- }
- if (is_array($name)) {
- $this->param = [];
- return $this->get = array_merge($this->get, $name);
- }
- return $this->input($this->get, $name, $default, $filter);
- }
- /**
- * 设置获取POST参数
- * @access public
- * @param mixed $name 变量名
- * @param mixed $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function post($name = '', $default = null, $filter = '')
- {
- if (empty($this->post)) {
- $content = $this->input;
- if (empty($_POST) && false !== strpos($this->contentType(), 'application/json')) {
- $this->post = (array) json_decode($content, true);
- } else {
- $this->post = $_POST;
- }
- }
- if (is_array($name)) {
- $this->param = [];
- return $this->post = array_merge($this->post, $name);
- }
- return $this->input($this->post, $name, $default, $filter);
- }
- /**
- * 设置获取PUT参数
- * @access public
- * @param mixed $name 变量名
- * @param mixed $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function put($name = '', $default = null, $filter = '')
- {
- if (is_null($this->put)) {
- $content = $this->input;
- if (false !== strpos($this->contentType(), 'application/json')) {
- $this->put = (array) json_decode($content, true);
- } else {
- parse_str($content, $this->put);
- }
- }
- if (is_array($name)) {
- $this->param = [];
- return $this->put = is_null($this->put) ? $name : array_merge($this->put, $name);
- }
- return $this->input($this->put, $name, $default, $filter);
- }
- /**
- * 设置获取DELETE参数
- * @access public
- * @param mixed $name 变量名
- * @param mixed $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function delete($name = '', $default = null, $filter = '')
- {
- return $this->put($name, $default, $filter);
- }
- /**
- * 设置获取PATCH参数
- * @access public
- * @param mixed $name 变量名
- * @param mixed $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function patch($name = '', $default = null, $filter = '')
- {
- return $this->put($name, $default, $filter);
- }
- /**
- * 获取request变量
- * @access public
- * @param mixed $name 数据名称
- * @param string $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function request($name = '', $default = null, $filter = '')
- {
- if (empty($this->request)) {
- $this->request = $_REQUEST;
- }
- if (is_array($name)) {
- $this->param = [];
- return $this->request = array_merge($this->request, $name);
- }
- return $this->input($this->request, $name, $default, $filter);
- }
- /**
- * 获取session数据
- * @access public
- * @param mixed $name 数据名称
- * @param string $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function session($name = '', $default = null, $filter = '')
- {
- if (empty($this->session)) {
- $this->session = Container::get('session')->get();
- }
- if (is_array($name)) {
- return $this->session = array_merge($this->session, $name);
- }
- return $this->input($this->session, $name, $default, $filter);
- }
- /**
- * 获取cookie参数
- * @access public
- * @param mixed $name 数据名称
- * @param string $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function cookie($name = '', $default = null, $filter = '')
- {
- $cookie = Container::get('cookie');
- if (empty($this->cookie)) {
- $this->cookie = $cookie->get();
- }
- if (is_array($name)) {
- return $this->cookie = array_merge($this->cookie, $name);
- } elseif (!empty($name)) {
- $data = $cookie->has($name) ? $cookie->get($name) : $default;
- } else {
- $data = $this->cookie;
- }
- // 解析过滤器
- $filter = $this->getFilter($filter, $default);
- if (is_array($data)) {
- array_walk_recursive($data, [$this, 'filterValue'], $filter);
- reset($data);
- } else {
- $this->filterValue($data, $name, $filter);
- }
- return $data;
- }
- /**
- * 获取server参数
- * @access public
- * @param mixed $name 数据名称
- * @param string $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function server($name = '', $default = null, $filter = '')
- {
- if (empty($this->server)) {
- $this->server = $_SERVER;
- }
- if (is_array($name)) {
- return $this->server = array_merge($this->server, $name);
- }
- return $this->input($this->server, false === $name ? false : strtoupper($name), $default, $filter);
- }
- /**
- * 获取上传的文件信息
- * @access public
- * @param string|array $name 名称
- * @return null|array|\think\File
- */
- public function file($name = '')
- {
- if (empty($this->file)) {
- $this->file = isset($_FILES) ? $_FILES : [];
- }
- if (is_array($name)) {
- return $this->file = array_merge($this->file, $name);
- }
- $files = $this->file;
- if (!empty($files)) {
- // 处理上传文件
- $array = [];
- foreach ($files as $key => $file) {
- if (is_array($file['name'])) {
- $item = [];
- $keys = array_keys($file);
- $count = count($file['name']);
- for ($i = 0; $i < $count; $i++) {
- if (empty($file['tmp_name'][$i]) || !is_file($file['tmp_name'][$i])) {
- continue;
- }
- $temp['key'] = $key;
- foreach ($keys as $_key) {
- $temp[$_key] = $file[$_key][$i];
- }
- $item[] = (new File($temp['tmp_name']))->setUploadInfo($temp);
- }
- $array[$key] = $item;
- } else {
- if ($file instanceof File) {
- $array[$key] = $file;
- } else {
- if (empty($file['tmp_name']) || !is_file($file['tmp_name'])) {
- continue;
- }
- $array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
- }
- }
- }
- if (strpos($name, '.')) {
- list($name, $sub) = explode('.', $name);
- }
- if ('' === $name) {
- // 获取全部文件
- return $array;
- } elseif (isset($sub) && isset($array[$name][$sub])) {
- return $array[$name][$sub];
- } elseif (isset($array[$name])) {
- return $array[$name];
- }
- }
- return;
- }
- /**
- * 获取环境变量
- * @access public
- * @param mixed $name 数据名称
- * @param string $default 默认值
- * @param string|array $filter 过滤方法
- * @return mixed
- */
- public function env($name = '', $default = null, $filter = '')
- {
- if (empty($this->env)) {
- $this->env = Container::get('env')->get();
- }
- if (is_array($name)) {
- return $this->env = array_merge($this->env, $name);
- }
- return $this->input($this->env, false === $name ? false : strtoupper($name), $default, $filter);
- }
- /**
- * 设置或者获取当前的Header
- * @access public
- * @param string|array $name header名称
- * @param string $default 默认值
- * @return string
- */
- public function header($name = '', $default = null)
- {
- if (empty($this->header)) {
- $header = [];
- if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
- $header = $result;
- } else {
- $server = $this->server ?: $_SERVER;
- foreach ($server as $key => $val) {
- if (0 === strpos($key, 'HTTP_')) {
- $key = str_replace('_', '-', strtolower(substr($key, 5)));
- $header[$key] = $val;
- }
- }
- if (isset($server['CONTENT_TYPE'])) {
- $header['content-type'] = $server['CONTENT_TYPE'];
- }
- if (isset($server['CONTENT_LENGTH'])) {
- $header['content-length'] = $server['CONTENT_LENGTH'];
- }
- }
- $this->header = array_change_key_case($header);
- }
- if (is_array($name)) {
- return $this->header = array_merge($this->header, $name);
- }
- if ('' === $name) {
- return $this->header;
- }
- $name = str_replace('_', '-', strtolower($name));
- return isset($this->header[$name]) ? $this->header[$name] : $default;
- }
- /**
- * 获取变量 支持过滤和默认值
- * @access public
- * @param array $data 数据源
- * @param string|false $name 字段名
- * @param mixed $default 默认值
- * @param string|array $filter 过滤函数
- * @return mixed
- */
- public function input($data = [], $name = '', $default = null, $filter = '')
- {
- if (false === $name) {
- // 获取原始数据
- return $data;
- }
- $name = (string) $name;
- if ('' != $name) {
- // 解析name
- if (strpos($name, '/')) {
- list($name, $type) = explode('/', $name);
- } else {
- $type = 's';
- }
- // 按.拆分成多维数组进行判断
- foreach (explode('.', $name) as $val) {
- if (isset($data[$val])) {
- $data = $data[$val];
- } else {
- // 无输入数据,返回默认值
- return $default;
- }
- }
- if (is_object($data)) {
- return $data;
- }
- }
- // 解析过滤器
- $filter = $this->getFilter($filter, $default);
- if (is_array($data)) {
- array_walk_recursive($data, [$this, 'filterValue'], $filter);
- reset($data);
- } else {
- $this->filterValue($data, $name, $filter);
- }
- if (isset($type) && $data !== $default) {
- // 强制类型转换
- $this->typeCast($data, $type);
- }
- return $data;
- }
- /**
- * 设置或获取当前的过滤规则
- * @access public
- * @param mixed $filter 过滤规则
- * @return mixed
- */
- public function filter($filter = null)
- {
- if (is_null($filter)) {
- return $this->filter;
- } else {
- $this->filter = $filter;
- }
- }
- protected function getFilter($filter, $default)
- {
- if (is_null($filter)) {
- $filter = [];
- } else {
- $filter = $filter ?: $this->filter;
- if (is_string($filter) && false === strpos($filter, '/')) {
- $filter = explode(',', $filter);
- } else {
- $filter = (array) $filter;
- }
- }
- $filter[] = $default;
- return $filter;
- }
- /**
- * 递归过滤给定的值
- * @access public
- * @param mixed $value 键值
- * @param mixed $key 键名
- * @param array $filters 过滤方法+默认值
- * @return mixed
- */
- private function filterValue(&$value, $key, $filters)
- {
- $default = array_pop($filters);
- foreach ($filters as $filter) {
- if (is_callable($filter)) {
- // 调用函数或者方法过滤
- $value = call_user_func($filter, $value);
- } elseif (is_scalar($value)) {
- if (false !== strpos($filter, '/')) {
- // 正则过滤
- if (!preg_match($filter, $value)) {
- // 匹配不成功返回默认值
- $value = $default;
- break;
- }
- } elseif (!empty($filter)) {
- // filter函数不存在时, 则使用filter_var进行过滤
- // filter为非整形值时, 调用filter_id取得过滤id
- $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter));
- if (false === $value) {
- $value = $default;
- break;
- }
- }
- }
- }
- return $value;
- }
- /**
- * 强制类型转换
- * @access public
- * @param string $data
- * @param string $type
- * @return mixed
- */
- private function typeCast(&$data, $type)
- {
- switch (strtolower($type)) {
- // 数组
- case 'a':
- $data = (array) $data;
- break;
- // 数字
- case 'd':
- $data = (int) $data;
- break;
- // 浮点
- case 'f':
- $data = (float) $data;
- break;
- // 布尔
- case 'b':
- $data = (boolean) $data;
- break;
- // 字符串
- case 's':
- default:
- if (is_scalar($data)) {
- $data = (string) $data;
- } else {
- throw new \InvalidArgumentException('variable type error:' . gettype($data));
- }
- }
- }
- /**
- * 是否存在某个请求参数
- * @access public
- * @param string $name 变量名
- * @param string $type 变量类型
- * @param bool $checkEmpty 是否检测空值
- * @return mixed
- */
- public function has($name, $type = 'param', $checkEmpty = false)
- {
- if (empty($this->$type)) {
- $param = $this->$type();
- } else {
- $param = $this->$type;
- }
- // 按.拆分成多维数组进行判断
- foreach (explode('.', $name) as $val) {
- if (isset($param[$val])) {
- $param = $param[$val];
- } else {
- return false;
- }
- }
- return ($checkEmpty && '' === $param) ? false : true;
- }
- /**
- * 获取指定的参数
- * @access public
- * @param string|array $name 变量名
- * @param string $type 变量类型
- * @return mixed
- */
- public function only($name, $type = 'param')
- {
- $param = $this->$type();
- if (is_string($name)) {
- $name = explode(',', $name);
- }
- $item = [];
- foreach ($name as $key => $val) {
- if (is_int($key)) {
- $default = null;
- $key = $val;
- } else {
- $default = $val;
- }
- if (isset($param[$key])) {
- $item[$key] = $param[$key];
- } elseif (isset($default)) {
- $item[$key] = $default;
- }
- }
- return $item;
- }
- /**
- * 排除指定参数获取
- * @access public
- * @param string|array $name 变量名
- * @param string $type 变量类型
- * @return mixed
- */
- public function except($name, $type = 'param')
- {
- $param = $this->$type();
- if (is_string($name)) {
- $name = explode(',', $name);
- }
- foreach ($name as $key) {
- if (isset($param[$key])) {
- unset($param[$key]);
- }
- }
- return $param;
- }
- /**
- * 当前是否ssl
- * @access public
- * @return bool
- */
- public function isSsl()
- {
- $server = array_merge($_SERVER, $this->server);
- if (isset($server['HTTPS']) && ('1' == $server['HTTPS'] || 'on' == strtolower($server['HTTPS']))) {
- return true;
- } elseif (isset($server['REQUEST_SCHEME']) && 'https' == $server['REQUEST_SCHEME']) {
- return true;
- } elseif (isset($server['SERVER_PORT']) && ('443' == $server['SERVER_PORT'])) {
- return true;
- } elseif (isset($server['HTTP_X_FORWARDED_PROTO']) && 'https' == $server['HTTP_X_FORWARDED_PROTO']) {
- return true;
- } elseif ($this->config->get('https_agent_name') && isset($server[$this->config->get('https_agent_name')])) {
- return true;
- }
- return false;
- }
- /**
- * 当前是否Ajax请求
- * @access public
- * @param bool $ajax true 获取原始ajax请求
- * @return bool
- */
- public function isAjax($ajax = false)
- {
- $value = $this->server('HTTP_X_REQUESTED_WITH', '', 'strtolower');
- $result = ('xmlhttprequest' == $value) ? true : false;
- if (true === $ajax) {
- return $result;
- } else {
- return $this->param($this->config->get('var_ajax')) ? true : $result;
- }
- }
- /**
- * 当前是否Pjax请求
- * @access public
- * @param bool $pjax true 获取原始pjax请求
- * @return bool
- */
- public function isPjax($pjax = false)
- {
- $result = !is_null($this->server('HTTP_X_PJAX')) ? true : false;
- if (true === $pjax) {
- return $result;
- } else {
- return $this->param($this->config->get('var_pjax')) ? true : $result;
- }
- }
- /**
- * 获取客户端IP地址
- * @access public
- * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
- * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
- * @return mixed
- */
- public function ip($type = 0, $adv = true)
- {
- $type = $type ? 1 : 0;
- static $ip = null;
- if (null !== $ip) {
- return $ip[$type];
- }
- if ($adv) {
- if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
- $pos = array_search('unknown', $arr);
- if (false !== $pos) {
- unset($arr[$pos]);
- }
- $ip = trim(current($arr));
- } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
- $ip = $_SERVER['HTTP_CLIENT_IP'];
- } elseif (isset($_SERVER['REMOTE_ADDR'])) {
- $ip = $_SERVER['REMOTE_ADDR'];
- }
- } elseif (isset($_SERVER['REMOTE_ADDR'])) {
- $ip = $_SERVER['REMOTE_ADDR'];
- }
- // IP地址合法验证
- $long = sprintf("%u", ip2long($ip));
- $ip = $long ? [$ip, $long] : ['0.0.0.0', 0];
- return $ip[$type];
- }
- /**
- * 检测是否使用手机访问
- * @access public
- * @return bool
- */
- public function isMobile()
- {
- if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
- return true;
- } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
- return true;
- } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
- return true;
- } elseif (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * 当前URL地址中的scheme参数
- * @access public
- * @return string
- */
- public function scheme()
- {
- return $this->isSsl() ? 'https' : 'http';
- }
- /**
- * 当前请求URL地址中的query参数
- * @access public
- * @return string
- */
- public function query()
- {
- return $this->server('QUERY_STRING');
- }
- /**
- * 当前请求的host
- * @access public
- * @return string
- */
- public function host()
- {
- if (isset($_SERVER['HTTP_X_REAL_HOST'])) {
- return $_SERVER['HTTP_X_REAL_HOST'];
- }
- return $this->server('HTTP_HOST');
- }
- /**
- * 当前请求URL地址中的port参数
- * @access public
- * @return integer
- */
- public function port()
- {
- return $this->server('SERVER_PORT');
- }
- /**
- * 当前请求 SERVER_PROTOCOL
- * @access public
- * @return integer
- */
- public function protocol()
- {
- return $this->server('SERVER_PROTOCOL');
- }
- /**
- * 当前请求 REMOTE_PORT
- * @access public
- * @return integer
- */
- public function remotePort()
- {
- return $this->server('REMOTE_PORT');
- }
- /**
- * 当前请求 HTTP_CONTENT_TYPE
- * @access public
- * @return string
- */
- public function contentType()
- {
- $contentType = $this->server('CONTENT_TYPE');
- if ($contentType) {
- if (strpos($contentType, ';')) {
- list($type) = explode(';', $contentType);
- } else {
- $type = $contentType;
- }
- return trim($type);
- }
- return '';
- }
- /**
- * 获取当前请求的路由信息
- * @access public
- * @param array $route 路由名称
- * @return array
- */
- public function routeInfo($route = [])
- {
- if (!empty($route)) {
- $this->routeInfo = $route;
- } else {
- return $this->routeInfo;
- }
- }
- /**
- * 设置或者获取当前请求的调度信息
- * @access public
- * @param array $dispatch 调度信息
- * @return array
- */
- public function dispatch($dispatch = null)
- {
- if (!is_null($dispatch)) {
- $this->dispatch = $dispatch;
- }
- return $this->dispatch;
- }
- /**
- * 设置或者获取当前的模块名
- * @access public
- * @param string $module 模块名
- * @return string|Request
- */
- public function module($module = null)
- {
- if (!is_null($module)) {
- $this->module = $module;
- return $this;
- } else {
- return $this->module ?: '';
- }
- }
- /**
- * 设置或者获取当前的控制器名
- * @access public
- * @param string $controller 控制器名
- * @return string|Request
- */
- public function controller($controller = null)
- {
- if (!is_null($controller)) {
- $this->controller = $controller;
- return $this;
- } else {
- return $this->controller ?: '';
- }
- }
- /**
- * 设置或者获取当前的操作名
- * @access public
- * @param string $action 操作名
- * @return string|Request
- */
- public function action($action = null)
- {
- if (!is_null($action)) {
- $this->action = $action;
- return $this;
- } else {
- return $this->action ?: '';
- }
- }
- /**
- * 设置或者获取当前的语言
- * @access public
- * @param string $lang 语言名
- * @return string|Request
- */
- public function langset($lang = null)
- {
- if (!is_null($lang)) {
- $this->langset = $lang;
- return $this;
- } else {
- return $this->langset ?: '';
- }
- }
- /**
- * 设置或者获取当前请求的content
- * @access public
- * @return string
- */
- public function getContent()
- {
- if (is_null($this->content)) {
- $this->content = $this->input;
- }
- return $this->content;
- }
- /**
- * 获取当前请求的php://input
- * @access public
- * @return string
- */
- public function getInput()
- {
- return $this->input;
- }
- /**
- * 生成请求令牌
- * @access public
- * @param string $name 令牌名称
- * @param mixed $type 令牌生成方法
- * @return string
- */
- public function token($name = '__token__', $type = 'md5')
- {
- $type = is_callable($type) ? $type : 'md5';
- $token = call_user_func($type, $_SERVER['REQUEST_TIME_FLOAT']);
- if ($this->isAjax()) {
- header($name . ': ' . $token);
- }
- Container::get('session')->set($name, $token);
- return $token;
- }
- /**
- * 设置当前地址的请求缓存
- * @access public
- * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
- * @param mixed $expire 缓存有效期
- * @param array $except 缓存排除
- * @param string $tag 缓存标签
- * @return void
- */
- public function cache($key, $expire = null, $except = [], $tag = null)
- {
- if (!is_array($except)) {
- $tag = $except;
- $except = [];
- }
- if (false !== $key && $this->isGet() && !$this->isCheckCache) {
- // 标记请求缓存检查
- $this->isCheckCache = true;
- if (false === $expire) {
- // 关闭当前缓存
- return;
- }
- foreach ($except as $rule) {
- if (0 === stripos($this->url(), $rule)) {
- return;
- }
- }
- if ($key instanceof \Closure) {
- $key = call_user_func_array($key, [$this]);
- } elseif (true === $key) {
- // 自动缓存功能
- $key = '__URL__';
- } elseif (strpos($key, '|')) {
- list($key, $fun) = explode('|', $key);
- }
- // 特殊规则替换
- if (false !== strpos($key, '__')) {
- $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__'], [$this->module, $this->controller, $this->action, md5($this->url(true))], $key);
- }
- if (false !== strpos($key, ':')) {
- $param = $this->param();
- foreach ($param as $item => $val) {
- if (is_string($val) && false !== strpos($key, ':' . $item)) {
- $key = str_replace(':' . $item, $val, $key);
- }
- }
- } elseif (strpos($key, ']')) {
- if ('[' . $this->ext() . ']' == $key) {
- // 缓存某个后缀的请求
- $key = md5($this->url());
- } else {
- return;
- }
- }
- if (isset($fun)) {
- $key = $fun($key);
- }
- $cache = Container::get('cache');
- if (strtotime($this->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $_SERVER['REQUEST_TIME']) {
- // 读取缓存
- $response = Response::create()->code(304);
- throw new HttpResponseException($response);
- } elseif ($cache->has($key)) {
- list($content, $header) = $cache->get($key);
- $response = Response::create($content)->header($header);
- throw new HttpResponseException($response);
- } else {
- $this->cache = [$key, $expire, $tag];
- }
- }
- }
- /**
- * 读取请求缓存设置
- * @access public
- * @return array
- */
- public function getCache()
- {
- return $this->cache;
- }
- }
|