zoukankan      html  css  js  c++  java
  • php tp6学习笔记(一) 如何获取请求参数

    请求对象

      介绍:对http请求参数的读取,不管是post还是get,都是通过使用请求对象类Request来实现的。文件目录(tp6:vendor opthinkframeworksrc hinkRequest.php)

      注入方法:

      1.通过构造函数注入

    <?php
    
    namespace appindexcontroller;
    
    use thinkRequest;
    
    class Index 
    {
        /**
         * @var 	hinkRequest Request实例
         */
        protected $request;
        
        /**
         * 构造方法
         * @param Request $request Request对象
         * @access public
         */
        public function __construct(Request $request)
        {
            $this->request = $request;
        }
        
        public function index()
        {
            return $this->request->param('name');
        }    
    }

      2.直接在方法中注入

    <?php
    
    namespace appindexcontroller;
    
    use thinkRequest;
    
    class Index
    {
        
        public function index(Request $request)
        {
            return $request->param('name');
        }    
    }

      3.通过静态方法调用

    <?php
    
    namespace appindexcontroller;
    
    use thinkfacadeRequest;
    
    class Index
    {
        
        public function index()
        {
            return Request::param('name');
        }    
    }

      4.助手函数

    <?php
    
    namespace appindexcontroller;
    
    
    class Index
    {
    
        public function index()
        {
            return request()->param('name');
        }
    }

    获取请求传参

      万能方法,可以获取任何请求的参数(除了文件(file)之外);file需要使用file()方法获取

      request->param()

    <?php
    
    
    namespace appcontroller;
    
    
    use appBaseController;
    use appRequest;
    
    class User extends BaseController
    {
        public function register(Request $req){
            $req->param();//获取所有参数,返回一个数组,里面是参数集合
            $req->param("name");//获取指定字段的参数
        }
    }

      指定请求类型获取,只能获取对应请求的参数

    get    获取 $_GET 变量
    post    获取 $_POST 变量
    put    获取 PUT 变量
    delete    获取 DELETE 变量
    session    获取 SESSION 变量
    cookie    获取 $_COOKIE 变量
    request    获取 $_REQUEST 变量
    server    获取 $_SERVER 变量
    env    获取 $_ENV 变量
    route    获取 路由(包括PATHINFO) 变量
    middleware    获取 中间件赋值/传递的变量
    file    获取 $_FILES 变量 //这个是比较常用的

    获取请求类型

      request->method()//获取当前请求类型

    <?php
    
    
    namespace appcontroller;
    
    
    use appBaseController;
    use appRequest;
    
    class User extends BaseController
    {
        public function register(Request $req){
           $requestType= $req->method();//永远返回大写字符串
            var_dump($requestType);
        }
    }
    获取当前请求类型    method
    判断是否GET请求    isGet
    判断是否POST请求    isPost
    判断是否PUT请求    isPut
    判断是否DELETE请求    isDelete
    判断是否AJAX请求    isAjax
    判断是否PJAX请求    isPjax
    判断是否JSON请求    isJson
    判断是否手机访问    isMobile
    判断是否HEAD请求    isHead
    判断是否PATCH请求    isPatch
    判断是否OPTIONS请求    isOptions
    判断是否为CLI执行    isCli
    判断是否为CGI模式    isCgi

    获取请求头数据

      request->header()

    <?php
    
    
    namespace appcontroller;
    
    
    use appBaseController;
    use appRequest;
    
    class User extends BaseController
    {
        public function register(Request $req){
            $heads= $req->header();//获取全部请求头
            $heads= $req->header('content-type');//获取指定请求头值
            var_dump($heads);
        }
    }
  • 相关阅读:
    常用CDN
    SQL语句小结
    jQuery源码解析----domManip
    服务治理 SpringCloud Eureka
    docker容器操作
    docker镜像操作常用命令
    Maven
    Centos6解决网络不可达
    MyBatis
    SpringMVC
  • 原文地址:https://www.cnblogs.com/wrhbk/p/14700916.html
Copyright © 2011-2022 走看看