TP5 请求信息与请求参数

栏目:建站技术 2020-10-30

<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
class Index extends Controller
{
    public function index()
    {    
        //index/index/index/name/zs/age/18/sex/nan
        //使用函数助手 不需要use think\Controller;不需要use think\Request;不需要extends Controller
        echo "<pre>";
        print_r(request()->param());//请求参数
        echo "<br/>";
        echo request()->param('name');//获取参数数组中的name参数的值
        echo "<br/>";
        print_r(request()->only(['name','age']));//请求参数:仅包含name和age';
        echo "<br/>";
        print_r(request()->except(['name']));//请求参数:排除name';
        echo "<br/>";
        //param方法支持变量的过滤和默认值,如果没有传en_name参数,那么en_name参数默认就是jake,如果有传,那么对其进行小写处理
        echo request()->param('user_name','jake','strtolower');
        echo "<br/>";
        request()->bind('user_age',30);//动态绑定属性,属性名是user_age
        echo request()->user_age;//获取动态绑定的user_age这个属性的值
        echo "<br/>";
        //但是要注意变量的过滤和默认值以及动态绑定属性并不会改变request()->param()的结果,可以通过下面的再来输出一个其值
        print_r(request()->param());//获取参数数组
        echo request()->param('user_name');//输出为空
        echo request()->param('user_age');//输出为空
        echo "<br/>";
        print_r(request()->get());//要获取值,需要用?&的方式比如/index/index/index.html?name=zs&age=18&sex=nan
        echo "<br/>";
        echo request()->get('name');//要获取值,需要用?&的方式比如/index/index/index.html?name=zs&age=18&sex=nan
        echo "<br/>";
        print_r(request()->post());
        echo "<br/>";
        echo request()->post('name');
        echo "<br/>";
        print_r(request()->cookie());
        echo "<br/>";
        echo request()->cookie('pgv_pvi');
        echo "<br/>";
        print_r(request()->file());
        echo "<br/>";
        echo request()->file('image');
        echo "<br/>";
        echo "<br/>请求方法:";
        echo request()->method();
        echo "<br/>访问IP:";
        echo request()->ip();
        echo "<br/>是否是AJAX请求:";
        echo request()->isAjax() ? '是':'否';
        echo "<br/>当前域名:";
        echo request()->domain();
        echo "<br/>当前入口文件:";
        echo request()->baseFile();
        echo "<br/>包含域名的完整URL地址:";
        echo request()->url(true);
        echo "<br/>不包含域名的完整URL地址:";
        echo request()->url();
        echo "<br/>URL地址的参数信息:";
        echo request()->query();
        echo "<br/>不包含参数信息G的URL地址:";
        echo request()->baseUrl();
        echo "<br/>URL地址中的pathinfo信息:";
        echo request()->pathinfo();
        echo "<br/>URL地址中的pathinfo信息 不含后缀:";
        echo request()->path();
        echo "<br/>URL地址中的后缀信息:";
        echo request()->ext();
        echo "<br/>URL访问的ROOT地址:";
        echo request()->root(true);//等价于request()->domain()
        echo "<br/>URL访问的ROOT地址:";
        echo request()->root();
        echo "<br/>当前模块名:";
        echo request()->module();
        echo "<br/>当前控件器名:";
        echo request()->controller();
        echo "<br/>当前方法名:";
        echo request()->action();
        echo "<br/>路由信息:";
        print_r(request()->route());
        echo "<br/>调度信息:";
        print_r(request()->dispatch());
        echo "<br/>";
        print_r(input());//与request()->param()等价
        echo "<br/>";
        echo input('name');//与request()->param('name')等价
        echo "<br/>";
        print_r(input('get.'));//要获取值,需要用?&的方式比如/index/index/index.html?name=zs&age=18
        echo "<br/>";
        echo input('get.name');//要获取值,需要用?&的方式比如/index/index/index.html?name=zs&age=18
        echo "<br/>";
        print_r(input('post.'));
        echo "<br/>";
        echo input('post.name');
        echo "<br/>";
        print_r(input('cookie.'));
        echo "<br/>";
        echo input('cookie.pgv_pvi');
        echo "<br/>";
        print_r(input('file'));
        echo "<br/>";
        echo input('file.image');

        //不用使用函数助手第一种方法: 需要引入Request的命名空间,即use think\Request;
        $request = Request::instance();
        print_r($request->param());

        //不用使用函数助手第二种方法: 需要引入Request的命名空间,即use think\Controller以及继承Controller;
        //Controller中定义了一个成员变量protected $request;并且在Controller构造方法中就对那个成员变量进行了处理,所以Index继承Controller时,当Index被实例化的时候就可以调用父类的成员变量
        print_r($this->request->param());

    }
}

相关阅读

中英文混和的字符串的ASCII与UNICODE编码与解码

2020-10-10184

//1:获取中英文混合字符的编码(ASCII码与UNICODE码)$str=&quot;官网制作:网巢网络&quot;;foreach(math($str)as$key=&gt;$value){if(preg_match(&#39;/^[x{4e00}-x{9fa5}]+$/u&#39;,$value)){//中文,注意中文这里暂时不转换,因为无法通过chr…

如何让你的PHP网站变得更安全

2020-10-1082

安全配置一(1)打开php的安全模式php的安全模式是个非常重要的内嵌的安全机制,能够控制一些php中的函数,比如system(),同时把很多文件操作函数进行了权限控制,也不允许对某些关键文件的文件,比如/etc/passwd,但是默认的php.ini是没有打开安全模式的,我们把它打开:safe_mode=on(2)用户组安全当safe_mode打开时,sa…

如何解决网站上传大文件的问题?

2020-10-10119

为了服务器的网络安全,避免因为超大文件导致服务器故障,所以对文件上传的大小限制是很有必要的,但是有些客户又有上传大文件的需求,比如上传pdf或者压缩包这种格式的文件,那么碰到这种情况下,如何解决这个问题呢?在浏览器输出phpinfo();查看LoadedConfigurationFile这个参数可以获取php.info的文件位置1:如果上传的文件太大,会导致…

小程序的下单与支付的业务流程

2020-10-10221

1:小程序携带商品信息调用第三方服务器的下单API2:第三方服务器下单API对小程序带来的商品作库存量检测3:如果检测通过,第三方服务器就会保存这个订单并且告之小程序下单成功4:小程序调用第三方服务器的支付api5:第三方服务器API调用微信的预订单API[WxPayApi::unifiedOrder](注意第三方服务器是无法完成支付的,这个订单实际是在微信…

小程序倒计时wxml与js

2020-10-1069

小程序倒计时jsPage({data:{windowHeight:654,maxtime:&quot;&quot;,isHiddenLoading:true,isHiddenToast:true,dataList:{},countDownDay:0,countDownHour:0,countDownMinute:0,countDownSecond:0,},/…

小程序列表多个批量倒计时

2020-10-10100

Page({onShow(){letthat=this;vardates={datetime:[{dat:0,name:&#39;zs&#39;},{dat:6,name:&#39;ls&#39;},{dat:10,name:&#39;ww&#39;}]}//console.log(dates)//数据letlen=dates.datetime.length…