💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# ThinkPHP6.0 请求 要使用请求对象必须使用门面方式( think\\facade\\Request类负责 )调用。 * * * ## ThinkPHP6 请求 * 要使用请求对象必须使用门面方式( think\\facade\\Request类负责 )调用 * 可以通过Request对象完成全局输入变量的检测、获取和安全过滤 * 支持`$_GET`、`$_POST`、`$_REQUEST`、`$_SERVER`、`$_SESSION`、`$_COOKIE`、`$_ENV`等系统变量,以及文件上传信息 ## 一、变量获取 方法说明param获取当前请求的变量get获取 $_GET 变量post 获取 $_POST 变量put 获取 PUT 变量delete 获取 DELETE 变量session 获取 SESSION 变量cookie 获取 $_COOKIE 变量request 获取 $_REQUEST 变量server 获取 $_SERVER 变量env 获取 $_ENV 变量route 获取 路由(包括PATHINFO) 变量middleware 获取 中间件赋值/传递的变量file 获取 $_FILES 变量 1、GET 请求 * `PARAM`类型变量是框架提供的用于自动识别当前请求的一种变量获取方式,是系统推荐的获取请求参数的方法 * `param`方法会把当前请求类型的参数和路由变量以及GET请求合并,并且路由变量是优先的 controller代码 > public function edit(){ > >     print\_r( $\_GET );    // 原生get接收 > >     print\_r( Request::param() ); // 获取当前请求的所有变量 > >     print\_r( Request::param('id') );    // 获取当前请求的id变量 > >     print\_r( Request::get() ); > > } view代码:index.html > 编辑 > > > > > >     function edit(id){ > >         layer.open({ > >             type: 2, > >             title: '添加', > >             shade: 0.3, > >             area: \['480px', '440px'\], > >             content: '/index.php/index/edit?id='+id > >         }); > >     } > > 2、POST 请求 controller代码 > public function edit(){ > >     $id = Request::param('id'); > >     $shop = Db::table('shop\_goods')->where('id',$id)->find(); > >     $cat = Db::table('shop\_cat')->where('status',1)->select(); > >     View::assign(\[ > >         'shop' => $shop, > >         'cat' => $cat > >     \]); > >     return View::fetch(); > > } > > public function edits(){ > >     // print\_r( Request::param() ); > >     // print\_r( Request::post() ); > > > >     $all = Request::param(); > >     $update = Db::table('shop\_goods')->where('id',$all\['id'\])->update($all); > >     if($update){ > >         echo json\_encode(\['code'=>0,'msg'=>'修改成功'\]); > >     }else{ > >         echo json\_encode(\['code'=>1,'msg'=>'修改失败'\]); > >     } > > } view代码:edit.html > > > > > > >     > >     > >     > > > > > >     > >         > >         > >             标题 > >             > >                 > >             > >         > >         > >             分类 > >             > >                 > >                     > >                     {volist name="cat" id="cat\_v"} > >                         {$cat\_v\['name'\]} > >                     {/volist} > >                 > >             > >         > >         > >             原价 > >             > >                 > >             > >         > >         > >             折扣 > >             > >                 > >             > >         > >         > >             库存 > >             > >                 > >             > >         > >         > >             状态 > >             > >                 > >                     开启 > >                     关闭 > >                 > >             > >         > >     > >     > >         > >             保存 > >         > >     > >     > >         layui.use(\['layer','form'\],function(){ > >             form = layui.form; > >             layer = layui.layer; > >             $ = layui.jquery; > >         }); > >         function save(){ > >             $.post('/index.php/Index/edits',$('form').serialize(),function(res){ > >                 if(res.code>0){ > >                     layer.alert(res.msg,{icon:2}); > >                 }else{ > >                     layer.msg(res.msg); > >                     setTimeout(function(){parent.window.location.reload();},1000); > >                 } > >             },'json'); > >         } > >     > > > > 3、变量修饰符 序号修饰符作用1 s 强制转换为字符串类型2d 强制转换为整型类型3 b 强制转换为布尔类型4 a 强制转换为数组类型5f 强制转换为浮点类型 ~~~ Request::get('id/d'); Request::post('name/s'); Request::param('price/f'); ~~~ ## 二、请求类型 方法说明method 获取当前请求类型has 判断传值是否存在isGet 判断是否GET请求isPost 判断是否POST请求isPut 判断是否PUT请求isDelete 判断是否DELETE请求isAjax 判断是否AJAX请求isPjax 判断是否PJAX请求isJson 判断是否JSON请求isMobile 判断是否手机访问isHead 判断是否HEAD请求isPatch 判断是否PATCH请求isOptions 判断是否OPTIONS请求isCli 判断是否为CLI执行isCgi 判断是否为CGI模式 1、method > public function edit(){ > >     if(Request::method() == 'POST'){ > >         // print\_r(Request::method());exit; > >         $all = Request::param(); > >         $update = Db::table('shop\_goods')->where('id',$all\['id'\])->update($all); > >         if($update){ > >             echo json\_encode(\['code'=>0,'msg'=>'修改成功'\]); > >         }else{ > >             echo json\_encode(\['code'=>1,'msg'=>'修改失败'\]); > >         } > >     }else{ > >         // print\_r(Request::method());exit; > >         $id = Request::param('id'); > >         $shop = Db::table('shop\_goods')->where('id',$id)->find(); > >         $cat = Db::table('shop\_cat')->where('status',1)->select(); > >         View::assign(\[ > >             'shop' => $shop, > >             'cat' => $cat > >         \]); > >         return View::fetch(); > >     } > > } ## 三、示例:增加数据 controller代码 > public function add(){ > >     if(Request::method() == 'POST'){ > >         $all = Request::param(); > >         $insert = Db::table('shop\_goods')->insert($all); > >         if($insert){ > >             echo json\_encode(\['code'=>0,'msg'=>'添加成功'\]); > >         }else{ > >             echo json\_encode(\['code'=>1,'msg'=>'添加失败'\]); > >         } > >     }else{ > >         $cat = Db::table('shop\_cat')->where('status',1)->select(); > >         View::assign(\[ > >             'cat' => $cat > >         \]); > >         return View::fetch(); > >     } > > } view代码:add.html > > > > > > >     > >     > >     > > > > > >     > >         > >             标题 > >             > >                 > >             > >         > >         > >             分类 > >             > >                 > >                     > >                     {volist name="cat" id="cat\_v"} > >                         {$cat\_v\['name'\]} > >                     {/volist} > >                 > >             > >         > >         > >             原价 > >             > >                 > >             > >         > >         > >             折扣 > >             > >                 > >             > >         > >         > >             库存 > >             > >                 > >             > >         > >         > >             状态 > >             > >                 > >                     开启 > >                     关闭 > >                 > >             > >         > >     > >     > >         > >             保存 > >         > >     > >     > >         layui.use(\['layer','form'\],function(){ > >             form = layui.form; > >             layer = layui.layer; > >             $ = layui.jquery; > >         }); > >         function save(){ > >             $.post('/index.php/Index/add',$('form').serialize(),function(res){ > >                 if(res.code>0){ > >                     layer.alert(res.msg,{icon:2}); > >                 }else{ > >                     layer.msg(res.msg); > >                     setTimeout(function(){parent.window.location.reload();},1000); > >                 } > >             },'json'); > >         } > >     > > > > ## 四、示例:删除数据 controller代码 > public function del(){ > >     $id = Request::param('id'); > >     $delete = Db::table('shop\_goods')->where('id',$id)->delete(); > >     if($delete){ > >         echo json\_encode(\['code'=>0,'msg'=>'删除成功'\]); > >     }else{ > >         echo json\_encode(\['code'=>1,'msg'=>'删除失败'\]); > >     } > > } view代码:index.html > 删除 > > > >     function del(id){ > >         layer.confirm('确定要删除吗?', { > >             icon:3, > >             btn: \['确定','取消'\] > >         }, function(){ > >             $.post('/index.php/index/del',{'id':id},function(res){ > >                 if(res.code>0){ > >                     layer.alert(res.msg,{icon:2}); > >                 }else{ > >                     layer.msg(res.msg); > >                     setTimeout(function(){window.location.reload();},1000); > >                 } > >             },'json'); > >         }); > >     } > > ## 五、请求信息 序号方法说明1host 当前访问域名或者IP2 scheme 当前访问协议3 port 当前访问的端口4remotePort当前请求的REMOTE_PORT5protocol 当前请求的SERVER_PROTOCOL6contentType 当前请求的CONTENT_TYPE7 domain 当前包含协议的域名8 subDomain 当前访问的子域名9 panDomain 当前访问的泛域名10 rootDomain 当前访问的根域名11 url当前完整URL12 baseUrl 当前URL(不含QUERY_STRING)13 query 当前请求的QUERY_STRING参数14 baseFile 当前执行的文件15root URL访问根地址16 rootUrl URL访问根目录17 pathinfo 当前请求URL的pathinfo信息(含URL后缀)18 ext 当前URL的访问后缀19 time 获取当前请求的时间20 type 当前请求的资源类型21 method 当前请求类型22 rule 当前请求的路由对象实例23 controller 当前请求的控制器名24 action 当前请求的操作名 ~~~ print_r( Request::host() ); print_r( Request::url() ); print_r( Request::controller() ); print_r( Request::action() ); ~~~ ## 六、HTTP头信息 * HTTP请求头信息的名称不区分大小写,并且\_会自动转换为- ~~~ print_r( Request::header() ); print_r( Request::header('accept_encoding') ); ~~~