您的位置:首页 > 编程语言 > PHP开发

[李景山php]每天TP5-20170201|thinkphp5-Request.php-4

2017-01-06 08:39 375 查看
/**
* 设置获取获取当前请求的参数
* @access public
* @param string|array  $name 变量名
* @param mixed         $default 默认值
* @param string|array  $filter 过滤方法
* @return mixed
*/
public function param($name = '', $default = null, $filter = null)
{// 设置 或者 获取 当前请求的参数
if (empty($this->param)) {// 如果当前的参数 为空
$method = $this->method(true);// 首先 获取方法 也就获取数据的类型
// 自动获取请求变量
switch ($method) {// 根据不同的情况 设置
case 'POST':// post 参数通过 post 方式进行获取
$vars = $this->post(false);
break;
case 'PUT':
case 'DELETE':
case 'PATCH':
$vars = $this->put(false);// 其它的通过 put 方式获取
break;
default:
$vars = [];// 默认初始化 空数据
}
// 当前请求参数和URL地址中的参数合并
$this->param = array_merge($this->get(false), $vars, $this->route(false));// 然后 合并 路由 get 及当前获取的参数
// 最后还都是大杂烩,
}
if (true === $name) {// 如果 名字 为真
// 获取包含文件上传信息的数组
$file = $this->file();// 包含文件
$data = array_merge($this->param, $file);// 合并参数 及文件,统称为 data
return $this->input($data, '', $default, $filter);// 返回 输入的数据
}
return $this->input($this->param, $name, $default, $filter);// 返回输入的参数
}

/**
* 设置获取获取路由参数
* @access public
* @param string|array  $name 变量名
* @param mixed         $default 默认值
* @param string|array  $filter 过滤方法
* @return mixed
*/
public function route($name = '', $default = null, $filter = null)
{// 设置获取路由参数
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 string|array  $name 变量名
* @param mixed         $default 默认值
* @param string|array  $filter 过滤方法
* @return mixed
*/
public function get($name = '', $default = null, $filter = null)
{// get 方式获取参数
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 string        $name 变量名
* @param mixed         $default 默认值
* @param string|array  $filter 过滤方法
* @return mixed
*/
public function post($name = '', $default = null, $filter = null)
{
if (empty($this->post)) {
$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);
}// 获取 post 数据

/**
* 设置获取获取PUT参数
* @access public
* @param string|array      $name 变量名
* @param mixed             $default 默认值
* @param string|array      $filter 过滤方法
* @return mixed
*/
public function put($name = '', $default = null, $filter = null)
{// 获取 PUT参数
if (is_null($this->put)) {// 类型1
$content = file_get_contents('php://input');// 这样的数据流读入
if (strpos($content, '":')) {// 如果有数据,应该是这个表单
$this->put = json_decode($content, true);// json 格式解析
} 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 string|array      $name 变量名
* @param mixed             $default 默认值
* @param string|array      $filter 过滤方法
* @return mixed
*/
public function delete($name = '', $default = null, $filter = null)
{// 设置获取 delete 参数
return $this->put($name, $default, $filter);
}

/**
* 设置获取获取PATCH参数
* @access public
* @param string|array      $name 变量名
* @param mixed             $default 默认值
* @param string|array      $filter 过滤方法
* @return mixed
*/
public function patch($name = '', $default = null, $filter = null)
{
return $this->put($name, $default, $filter);
}// 同上的 patch

/**
* 获取request变量
* @param string        $name 数据名称
* @param string        $default 默认值
* @param string|array  $filter 过滤方法
* @return mixed
*/
public function request($name = '', $default = null, $filter = null)
{// 请求参数
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 string|array  $name 数据名称
* @param string        $default 默认值
* @param string|array  $filter 过滤方法
* @return mixed
*/
public function session($name = '', $default = null, $filter = null)
{// session 字段
if (empty($this->session)) {
$this->session = Session::get();
}// 设置session 字段
if (is_array($name)) {
return $this->session = array_merge($this->session, $name);
}// 数组 赋值 组合 返回 一气呵成 这个倒是不错,哈哈
return $this->input($this->session, $name, $default, $filter);
}

/**
* 获取cookie参数
* @access public
* @param string|array  $name 数据名称
* @param string        $default 默认值
* @param string|array  $filter 过滤方法
* @return mixed
*/
public function cookie($name = '', $default = null, $filter = null)
{// cookie 参数
if (empty($this->cookie)) {
$this->cookie = $_COOKIE;
}
if (is_array($name)) {
return $this->cookie = array_merge($this->cookie, $name);
}
return $this->input($this->cookie, $name, $default, $filter);
}// 同上 类似

/**
* 获取server参数
* @access public
* @param string|array  $name 数据名称
* @param string        $default 默认值
* @param string|array  $filter 过滤方法
* @return mixed
*/
public function server($name = '', $default = null, $filter = null)
{// 获取 server 参数
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])) {
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'])) {
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 null;// 默认 返回空
}

/**
* 获取环境变量
* @param string|array  $name 数据名称
* @param string        $default 默认值
* @param string|array  $filter 过滤方法
* @return mixed
*/
public function env($name = '', $default = null, $filter = null)
{// 获取环境变量
if (empty($this->env)) {// 如果为空
$this->env = $_ENV;
}
if (is_array($name)) {
return $this->env = array_merge($this->env, $name);
}
return $this->input($this->env, false === $name ? false : strtoupper($name), $default, $filter);
}// 同上的 其它方式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php5 thingk