Article directory
ThinkPHP6 request
- To use the request object, you must use the facade method (think\facade\Request class is responsible) to call
- The detection, acquisition and security filtering of global input variables can be completed through the Request object
- support
$_GET、$_POST、$_REQUEST、$_SERVER、$_SESSION、$_COOKIE、$_ENV等系统变量,以及文件上传信息
1. Variable acquisition
serial number | method | illustrate |
---|---|---|
1 | param | Get the variables of the current request |
2 | get | Get the $_GET variable |
3 | post | Get the $_POST variable |
4 | put | get PUT variable |
5 | delete | get DELETE variable |
6 | session | get session variables |
7 | cookie | Get the $_COOKIE variable |
8 | request | Get the $_REQUEST variable |
9 | server | Get the $_SERVER variable |
10 | env | Get the $_ENV variable |
11 | route | Get route (including PATHINFO) variables |
12 | middleware | Get the variables assigned/passed by middleware |
13 | file | Get the $_FILES variable |
14 | filter | variable filtering |
15 | only | Get some variables |
16 | except | Exclude some variables |
1. GET request
- The PARAM type variable is a variable acquisition method provided by the framework to automatically identify the current request, and is the method recommended by the system to obtain request parameters
- The param method will combine the parameters of the current request type with routing variables and GET requests, and routing variables are prioritized
controller code
public function edit(){
print_r( $_GET ); // 原生get接收
print_r( Request::param() ); // 获取当前请求的所有变量
print_r( Request::param('id') ); // 获取当前请求的id变量
print_r( Request::get() );
}
View code: index.html
<button class="layui-btn layui-btn-xs" onclick="edit({
$right_v.id})">编辑</button>
<script type="text/javascript">
function edit(id){
layer.open({
type: 2,
title: '添加',
shade: 0.3,
area: ['480px', '440px'],
content: '/index.php/index/edit?id='+id
});
}
</script>
2. POST request
controller code
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 code: edit.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
<script type="text/javascript" src="/static/layui/layui.js"></script>
</head>
<body style="padding:10px;">
<form class="layui-form">
<input type="hidden" name="id" value="{$shop.id}">
<div class="layui-form-item">
<label class="layui-form-label">标题</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="title" value="{$shop.title}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">分类</label>
<div class="layui-input-inline">
<select name="cat">
<option value=0 {if $shop['cat']==0} selected {/if}></option>
{volist name="cat" id="cat_v"}
<option value="{$cat_v['id']}" {if $shop['cat']==$cat_v['id']} selected {/if}>{$cat_v['name']}</option>
{/volist}
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">原价</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="price" value="{$shop.price}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">折扣</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="discount" value="{$shop.discount}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">库存</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="stock" value="{$shop.stock}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状态</label>
<div class="layui-input-inline">
<select name="status">
<option value="1" {if $shop['status']==1} selected {/if}>开启</option>
<option value="0" {if $shop['status']==0} selected {/if}>关闭</option>
</select>
</div>
</div>
</form>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" onclick="save()">保存</button>
</div>
</div>
<script type="text/javascript">
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');
}
</script>
</body>
</html>
3. Variable modifiers
serial number | Modifier | effect |
---|---|---|
1 | s | cast to string type |
2 | d | cast to an integral type |
3 | b | coerce to boolean type |
4 | a | cast to array type |
5 | f | coerce to float type |
Request::get('id/d');
Request::post('name/s');
Request::param('price/f');
2. Request type
serial number | method | illustrate |
---|---|---|
1 | method | Get the current request type |
2 | has | Determine whether the passed value exists |
3 | isGet | Determine whether a GET request |
4 | isPost | Determine whether a POST request |
5 | isPut | Determine whether the PUT request |
6 | isDelete | Determine whether to DELETE request |
7 | isAjax | Determine whether an AJAX request |
8 | I'm sorry | Determine whether a PJAX request |
9 | isJson | Determine whether JSON request |
10 | isMobile | Determine whether mobile phone access |
11 | isHead | Determine whether HEAD request |
12 | isPatch | 判断是否PATCH请求 |
13 | isOptions | 判断是否OPTIONS请求 |
14 | isCli | 判断是否为CLI执行 |
15 | 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代码:edit.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
<script type="text/javascript" src="/static/layui/layui.js"></script>
</head>
<body style="padding:10px;">
<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">标题</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="title" value="">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">分类</label>
<div class="layui-input-inline">
<select name="cat">
<option value=0 selected></option>
{volist name="cat" id="cat_v"}
<option value="{$cat_v['id']}">{$cat_v['name']}</option>
{/volist}
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">原价</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="price" value="">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">折扣</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="discount" value="">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">库存</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="stock" value="">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状态</label>
<div class="layui-input-inline">
<select name="status">
<option value="1" selected>开启</option>
<option value="0">关闭</option>
</select>
</div>
</div>
</form>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" onclick="save()">保存</button>
</div>
</div>
<script type="text/javascript">
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');
}
</script>
</body>
</html>
四、示例:删除数据
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代码:edit.html
<button class="layui-btn layui-btn-danger layui-btn-xs" onclick="del({
$right_v.id})">删除</button>
<script type="text/javascript">
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');
});
}
</script>
五、请求信息
序号 | 方法 | 说明 |
---|---|---|
1 | host | 当前访问域名或者IP |
2 | scheme | 当前访问协议 |
3 | port | 当前访问的端口 |
4 | remotePort | 当前请求的REMOTE_PORT |
5 | protocol | 当前请求的SERVER_PROTOCOL |
6 | contentType | 当前请求的CONTENT_TYPE |
7 | domain | 当前包含协议的域名 |
8 | subDomain | 当前访问的子域名 |
9 | panDomain | 当前访问的泛域名 |
10 | rootDomain | 当前访问的根域名 |
11 | url | 当前完整URL |
12 | baseUrl | 当前URL(不含QUERY_STRING) |
13 | query | 当前请求的QUERY_STRING参数 |
14 | baseFile | 当前执行的文件 |
15 | root | 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') );