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

MethodDescription
paramGet the variables of the current request
getGet 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 parameters

  • paramThe 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="/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="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 number

Modifier

Function

1

s

Forcing to string type

2d Forcing to integer type3 b Force to Boolean type4 a Force Convert to array type5f Force conversion to floating point type


Request::get('id/d');
Request::post('name/s');
Request::param('price/f');

2. Request type

##isGet Determine whether a GET request is made##isPost isPut isDelete isAjax isPjax isJson isMobile isHead isPatch isOptions isCli isCgi

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

<!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="2">关闭</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代码:index.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>

5. Request information

MethodDescription
method Get the current request type
has Determine whether the passed value exists
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
##12 baseUrl Current URL (excluding QUERY_STRING) 13 query QUERY_STRING parameter of the current request14 baseFile Currently executed file15root URL access root address16 rootUrl URL access root directory17 pathinfo The pathinfo information of the current requested URL (including URL suffix)18 ext The access suffix of the current URL19 time Get the time of the current request20 type Currently requested resource type##24 action The operation name of the current request
print_r( Request::host() );
print_r( Request::url() );
print_r( Request::controller() );
print_r( Request::action() );
Serial numberMethodDescription
1host Current access domain name or IP
2 scheme Current access protocol
3 port Currently accessed port
4remotePort REMOTE_PORT currently requested
5protocol SERVER_PROTOCOL currently requested
6contentType 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 urlCurrent complete URL
21 method Current request type
22 rule Current Requested routing object instance
23 controller Current requested controller name
6. HTTP header information

  • 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') );