ThinkPHP6.0 request
ThinkPHP6 request
To use the request object, you must use the facade method (think\facade\Request class is responsible) to call
You can use Request The object completes the detection, acquisition and security filtering of global input variables
supports
$_GET
,$_POST
,$_REQUEST
,$_SERVER
,$_SESSION
,$_COOKIE
,$_ENV
and other system variables, as well as file upload information
1. Variable acquisition
Method | Description |
param | Get the variables of the current request |
get | Get the $_GET variable |
post | Get the $_POST variable |
put | Get the PUT variable |
delete | Get the DELETE variable |
session | Get the SESSION variable |
cookie | Get the $_COOKIE variable |
request | Get the $_REQUEST variable |
server | Get the $_SERVER variable |
env | Get the $_ENV variable |
route | Get the route (including PATHINFO) variable |
middleware | Get the variables assigned/passed by the middleware |
file | Get the $_FILES variable |
1. GET request
PARAM
type variable is a variable acquisition method provided by the framework to automatically identify the current request. It is a system Recommended method of obtaining request parametersparam
The method will merge the parameters of the current request type with routing variables and GET requests, and routing variables will take precedence
controller code
public function edit(){
print_r( $_GET ); // Native get reception
print_r( Request::param() ); // Get all variables of the current request
print_r( Request::param('id') ); // Get the id variable of the current request
print_r( Request::get() );
}
view code: index.html
<button class="layui- btn layui-btn-xs" onclick="edit({$right_v.id})">Edit</button>
<script type="text /javascript">
function edit(id){
layer.open({
type: 2,
title: 'Add',
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'=>'Modification successful']);
}else{
echo json_encode(['code'=>1,'msg'=>'Modification failed']);
}
}
view Code: edit.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="//m.sbmmt.com/m/static/layui/css/layui.css">
<script type="text/javascript" src="//m.sbmmt.com/m/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="2" {if $shop['status']==2} 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;
## through { ’ ‐ ’ ‐ before before T T T ‐ T ‐ ‐ 1 out out out out of result to ## </script></body></html>3. Variable modifier
Serial numberModifierFunction1 s Forcing to string type
Forcing to integer type | 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Force to Boolean type | 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Force Convert to array type | 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Force conversion to floating point type |
Method | Description | |
method | Get the current request type | |
has | Determine whether the passed value exists | |
Determine whether a GET request is made | ||
Determine whether a POST request is made | ||
Judge whether PUT request | ||
Judge whether DELETE request | ||
Judge whether AJAX request | ||
Determine whether it is a PJAX request | ||
Determine whether it is a JSON request | ||
Determine whether mobile phone access | ||
Determine whether HEAD request | ||
Judge whether PATCH request | ||
Judge whether OPTIONS request | ||
Determine whether it is CLI execution | ||
Determine whether it is CGI mode |
Serial number | Method | Description |
1 | host | Current access domain name or IP |
2 | scheme | Current access protocol |
3 | port | Currently accessed port |
4 | remotePort | REMOTE_PORT currently requested |
5 | protocol | SERVER_PROTOCOL currently requested |
6 | contentType | The currently requested CONTENT_TYPE |
7 | domain | Currently contains the protocol Domain name |
8 | subDomain | Currently visited subdomain name |
9 | panDomain | Currently visited pan-domain name |
10 | rootDomain | Currently visited root domain name |
11 | url | Current complete URL |
baseUrl | Current URL (excluding QUERY_STRING) | |
query | QUERY_STRING parameter of the current request | |
baseFile | Currently executed file | |
root | URL access root address | |
rootUrl | URL access root directory | |
pathinfo | The pathinfo information of the current requested URL (including URL suffix) | |
ext | The access suffix of the current URL | |
time | Get the time of the current request | |
type | Currently requested resource type | |
21 | method | Current request type |
22 | rule | Current Requested routing object instance |
23 | controller | Current requested controller name |
action | The operation name of the current request |
- HTTP request header information names are not case-sensitive, and _ will be automatically converted to -
print_r( Request::header() ); print_r( Request::header('accept_encoding') );