博客列表 >6.【TP6学习笔记】tp中Request请求对象调用详解

6.【TP6学习笔记】tp中Request请求对象调用详解

 一纸荒凉* Armani
 一纸荒凉* Armani原创
2021年05月29日 15:17:151580浏览

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

<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 请求

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

<!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;        });        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、变量修饰符

序号修饰符作用
1s强制转换为字符串类型
2d强制转换为整型类型
3b强制转换为布尔类型
4a强制转换为数组类型
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 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();    }}

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">        <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;        });        function save(){            $.post('/index.php/Index/edit',$('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 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

{include file="public/head" /}{include file="public/left" /}<div class="main" style="padding:10px;">    <div class="content">        <span>商品列表</span>        <button class="layui-btn" onclick="add()">添加</button>        <div></div>    </div>    <table class="layui-table">        <thead>            <tr>                <th>ID</th>                <th>商品标题</th>                <th>分类</th>                <th>原价</th>                <th>折扣</th>                <th>现价</th>                <th>库存</th>                <th>状态</th>                <th>添加时间</th>                <th>操作</th>            </tr>        </thead>        <tbody>            {volist name="right" id="right_v"}                <tr>                    <td>{$right_v.id}</td>                    <td>{$right_v.title}</td>                    <td>{$right_v.cat}</td>                    <td>{$right_v.price}</td>                    <td>{$right_v.discount}</td>                    <td>                        {if $right_v.discount!=0}                            {$right_v.price*($right_v.discount/10)}                        {else/}                            {$right_v.price}                        {/if}                    </td>                    <td>{$right_v.stock}</td>                    <td>{if $right_v['status']==1}开启{else/}关闭{/if}</td>                    <td>{$right_v.add_time|date='Y-m-d'}</td>                    <td>                        <button class="layui-btn layui-btn-xs" onclick="edit({$right_v.id})">编辑</button>                        <button class="layui-btn layui-btn-danger layui-btn-xs" onclick="del({$right_v.id})">删除</button>                    </td>                </tr>            {/volist}        </tbody>    </table></div>{include file="public/bottom" /}<script type="text/javascript">    function add(){        layer.open({            type: 2,            title: '添加',            shade: 0.3,            area: ['480px', '440px'],            content: '/index.php/index/add'        });    }    function edit(id){        layer.open({            type: 2,            title: '编辑',            shade: 0.3,            area: ['480px', '440px'],            content: '/index.php/index/edit?id='+id        });    }    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>

六、请求信息

序号方法说明
1host当前访问域名或者IP
2scheme当前访问协议
3port当前访问的端口
4remotePort当前请求的REMOTE_PORT
5protocol当前请求的SERVER_PROTOCOL
6contentType当前请求的CONTENT_TYPE
7domain当前包含协议的域名
8subDomain当前访问的子域名
9panDomain当前访问的泛域名
10rootDomain当前访问的根域名
11url当前完整URL
12baseUrl当前URL(不含QUERY_STRING)
13query当前请求的QUERY_STRING参数
14baseFile当前执行的文件
15rootURL访问根地址
16rootUrlURL访问根目录
17pathinfo当前请求URL的pathinfo信息(含URL后缀)
18ext当前URL的访问后缀
19time获取当前请求的时间
20type当前请求的资源类型
21method当前请求类型
22rule当前请求的路由对象实例
23controller当前请求的控制器名
24action当前请求的操作名
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') );
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议