【TP5 :请求】请求信息

版权声明:本文为ywcmoon原创文章,未经允许不得转载。 https://blog.csdn.net/qq_39251267/article/details/82622756

请求信息

使用 \think\Request 类获取请求信息

//初始化
$request = Request::instance();

或助手函数

$request = request();

最方便的还是使用注入请求对象的方式来获取变量

获取URL信息

//例,http://tp5.com/index/index/hello.html?name=thinkphp

 $request = Request::instance();

 $request->domain()  // 获取当前域名  (http://tp5.com)

 $request->baseFile()  // 获取当前入口文件  (/index.php)

 $request->url()  // 获取当前URL地址 不含域名  (/index/index/hello.html?name=thinkphp)

 $request->url(true)  // 获取包含域名的完整URL地址  (http://tp5.com/index/index/hello.html?name=thinkphp)

 $request->baseUrl() // 获取当前URL地址 不含QUERY_STRING (/index/index/hello.html)

 $request->root()  // 获取URL访问的ROOT地址  

 $request->root(true) // 获取URL访问的ROOT地址  (http://tp5.com)

 $request->pathinfo() // 获取URL地址中的PATH_INFO信息  (index/index/hello.html)

 $request->path() // 获取URL地址中的PATH_INFO信息 不含后缀  (index/index/hello)

 $request->ext() // 获取URL地址中的后缀信息  (html)

设置 / 获取 => 模块 / 控制器 / 操作 的名称

//例,http://serverName/index.php/index/hello_world/index

$request = Request::instance();

//获取名称
$request->module();  //当前模块名称  (index)
$request->controller();  //当前控制器名称  (HelloWorld)
$request->action();  //当前操作名称  (index)

//设置名称
$request->module('module'); 
$request->controller('controller');  
$request->action('action');  /

获取请求参数

$request = Request::instance();

$request->method()  //请求方法  (GET)
$request->type()   //资源类型  (html)
$request->ip()  //问ip地址  (127.0.0.1)
$request->isAjax()   //是否AJax请求  (false)
$request->param()  

/*请求参数:
array (size=2)
  'test' => string 'ddd' (length=3)
  'name' => string 'thinkphp' (length=8)
*/

$request->only(['name'])   //请求参数:仅包含name
$request->except(['name'])   //请求参数:排除name

获取路由和调度信息


//修改hello方法,http://serverName/hello/thinkphp

$request = Request::instance();

$request->route()  //路由信息

array (size=4)
  'rule' => string 'hello/:name' (length=11)
  'route' => string 'index/hello' (length=11)
  'pattern' => 
    array (size=1)
      'name' => string '\w+' (length=3)
  'option' => 
    array (size=0)
      empty



$request->dispatch()  //调度信息

array (size=2)
  'type' => string 'module' (length=6)
  'module' => 
    array (size=3)
      0 => null
      1 => string 'index' (length=5)
      2 => string 'hello' (length=5)

路由定义为:

return [
    'hello/:name' =>['index/hello',[],['name'=>'\w+']],
];

设置请求信息

$request = Request::instance();
$request->root('index.php');
$request->pathinfo('index/index/hello');

猜你喜欢

转载自blog.csdn.net/qq_39251267/article/details/82622756
今日推荐