5-7请求对象与参数绑定:按名称和按顺序访问变量

5-7请求对象与参数绑定:按名称和按顺序访问变量

谈箔撮每惨扔啪位储花粱客妊

5-7请求对象与参数绑定:按名称和按顺序访问变量第1张

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function inedx($name)
   {
        return "萌面人资料铺";
   }
   public function hello($name,$lession)
   {
       return "我是".$name."正在学习".$lession."课程";
   }

}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第2张


Request请求类的位置:D:\phpStudy\PHPTutorial\WWW\tp5\thinkphp\library\think\Request.php

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function inedx($name)
   {
        return "萌面人资料铺";
   }
   public function hello($name,$lession)
   {
       return "我是".$name."正在学习".$lession."课程";
   }

   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的get请求变量
       dump($request->get());
   }
}

执行,通过get传参的形式访问hello()

5-7请求对象与参数绑定:按名称和按顺序访问变量第3张

通过get传参的形式访问demo

5-7请求对象与参数绑定:按名称和按顺序访问变量第4张


获取post请求

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function inedx($name)
   {
        return "萌面人资料铺";
   }
   public function hello($name,$lession)
   {
       return "我是".$name."正在学习".$lession."课程";
   }

   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->post());
   }
}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第5张

模拟post请求

5-7请求对象与参数绑定:按名称和按顺序访问变量第6张


param()可以获取所有请求类型包括get,post,path_info等

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function inedx($name)
   {
        return "萌面人资料铺";
   }
   public function hello($name,$lession)
   {
       return "我是".$name."正在学习".$lession."课程";
   }

   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->param());
   }
}

获取gei()传递的数据

5-7请求对象与参数绑定:按名称和按顺序访问变量第7张

获取post提交的数据

5-7请求对象与参数绑定:按名称和按顺序访问变量第8张

tp5推荐使用param获取参数


获取name值

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function inedx($name)
   {
        return "萌面人资料铺";
   }
   public function hello($name,$lession)
   {
       return "我是".$name."正在学习".$lession."课程";
   }

   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->param("name"));
   }
}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第9张


has()检查是否存在请求变量

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function inedx($name)
   {
        return "萌面人资料铺";
   }
   public function hello($name,$lession)
   {
       return "我是".$name."正在学习".$lession."课程";
   }

   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->has("name"));
   }
}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第10张

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function inedx($name)
   {
        return "萌面人资料铺";
   }
   public function hello($name,$lession)
   {
       return "我是".$name."正在学习".$lession."课程";
   }

   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->has("salary"));
   }
}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第11张


5-7请求对象与参数绑定:按名称和按顺序访问变量第12张

1.获取域名

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function inedx($name)
   {
        return "萌面人资料铺";
   }
   public function hello($name,$lession)
   {
       return "我是".$name."正在学习".$lession."课程";
   }

   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->domain());
   }
}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第13张

2.获取URL,不包括域名

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function inedx($name)
   {
        return "萌面人资料铺";
   }
   public function hello($name,$lession)
   {
       return "我是".$name."正在学习".$lession."课程";
   }

   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->url());
   }
}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第14张

3.传入参数true,包含域名:

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function inedx($name)
   {
        return "萌面人资料铺";
   }
   public function hello($name,$lession)
   {
       return "我是".$name."正在学习".$lession."课程";
   }

   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->url(true));
   }
}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第15张4.path_info()

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->pathinfo());
   }
}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第16张

url请求加后缀

5-7请求对象与参数绑定:按名称和按顺序访问变量第17张5.path()方法不包括后缀

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->path());
   }
}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第18张6.ext()只返回后缀信息

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->ext());
   }
}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第19张7.module()查看当前访问的模块

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->module());
   }
}

执行:


5-7请求对象与参数绑定:按名称和按顺序访问变量第20张8.controller查看当前的控制器

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->controller());
   }
}

执行:


5-7请求对象与参数绑定:按名称和按顺序访问变量第21张9.action()查看当前的操作

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       dump($request->action());
   }
}

执行:


5-7请求对象与参数绑定:按名称和按顺序访问变量第22张模块,控制器,操作都可以重新设置:

重新设置操作:

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{

    public function test()
    {
        return "我是test()方法";
    }
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       //参数为空,获取所有的post请求变量
       $request->action("test");
       dump($request->action());
   }
}

执行:


5-7请求对象与参数绑定:按名称和按顺序访问变量第23张10.method()查看请求类型

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       dump($request->method());
   }
}

执行:


5-7请求对象与参数绑定:按名称和按顺序访问变量第24张11.查看请求的ip

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       dump($request->ip());
   }
}

执行:

5-7请求对象与参数绑定:按名称和按顺序访问变量第25张12.only()请求对象只获取id

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       dump($request->only('id'));
   }
}

执行

5-7请求对象与参数绑定:按名称和按顺序访问变量第26张13.except()查看除了id之外的请求变量

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       dump($request->except("id"));
   }
}

执行:


5-7请求对象与参数绑定:按名称和按顺序访问变量第27张


5-7请求对象与参数绑定:按名称和按顺序访问变量第28张

复制惯例配置文件中的代码:

5-7请求对象与参数绑定:按名称和按顺序访问变量第29张

5-7请求对象与参数绑定:按名称和按顺序访问变量第30张

<?php
return [
    // URL参数方式 0 按名称成对解析 1 按顺序解析
    'url_param_type'         => 1,
];

D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:

<?php
namespace app\index\controller;
//先继承基类
class Index extends \think\Controller
{
   public function demo($id="",$name="",$age=18)
   {
       //调用请求类获取请求对象
       $request=\think\Request::instance();
       dump($request->param());
   }
}

url通过顺序解析

5-7请求对象与参数绑定:按名称和按顺序访问变量第31张


5-7请求对象与参数绑定:按名称和按顺序访问变量第32张


次倒伙庇芦妒兴部杠善郴档诧