TP5 learning: request

Posted by tobykw13 on Mon, 18 Nov 2019 18:04:03 +0100

I. request information

If you want to get the current request information, you can use the \ think\Request class,

except

$request = Request::instance();

You can also use helper functions

$request = request();

Of course, the most convenient way to get variables is to inject the request object.

Get URL information

$request = Request::instance();
// Get current domain name
echo 'domain: ' . $request->domain() . '<br/>';
// Get current entry file
echo 'file: ' . $request->baseFile() . '<br/>';
// Get the current URL address without domain name
echo 'url: ' . $request->url() . '<br/>';
// Get the full URL address containing the domain name
echo 'url with domain: ' . $request->url(true) . '<br/>';
// Get the current URL address without query string
echo 'url without query: ' . $request->baseUrl() . '<br/>';
// Get the ROOT address of URL access
echo 'root:' . $request->root() . '<br/>';
// Get the ROOT address of URL access
echo 'root with domain: ' . $request->root(true) . '<br/>';
// Get path info information in URL address
echo 'pathinfo: ' . $request->pathinfo() . '<br/>';
// Get path info in URL address without suffix
echo 'pathinfo: ' . $request->path() . '<br/>';
// Get suffix information in URL address
echo 'ext: ' . $request->ext() . '<br/>';

The output result is:

domain: http://tp5.com
file: /index.php
url: /index/index/hello.html?name=thinkphp
url with domain: http://tp5.com/index/index/hello.html?name=thinkphp
url without query: /index/index/hello.html
root:
root with domain: http://tp5.com
pathinfo: index/index/hello.html
pathinfo: index/index/hello
ext: html

Set / get module / controller / operation name

$request = Request::instance();
echo "The current module name is" . $request->module();
echo "The current controller name is" . $request->controller();
echo "The current operation name is" . $request->action();

If the current access address is http://serverName/index.php/index/hello_world/index

The output result is:

The current module name is index
 The current controller name is HelloWorld
 The current operation name is index

To set the module name value, you need to pass in the name to the module method, which is also used to set the controller name and operation name

Request::instance()->module('module_name');

Get request parameters

$request = Request::instance();
echo 'Request method:' . $request->method() . '<br/>';
echo 'Resource type:' . $request->type() . '<br/>';
echo 'Visit ip Address:' . $request->ip() . '<br/>';
echo 'Whether AJax Request:' . var_export($request->isAjax(), true) . '<br/>';
echo 'Request parameters:';
dump($request->param());
echo 'Request parameters: include only name';
dump($request->only(['name']));
echo 'Request parameters: exclude name';
dump($request->except(['name']));

The output result is:

Request method: GET
 Resource type: html
 Access ip address: 127.0.0.1
 Ajax request or not: false
 Request parameters:
array (size=2)
  'test' => string 'ddd' (length=3)
  'name' => string 'thinkphp' (length=8)
  
Request parameters: name only
array (size=1)
  'name' => string 'thinkphp' (length=8)
  
Request parameter: exclude name
array (size=1)
  'test' => string 'ddd' (length=3)

Get routing and scheduling information

The hello method is modified as follows:

$request = Request::instance();
echo 'Routing information:';
dump($request->route());
echo 'Scheduling information:';
dump($request->dispatch());

Routes are defined as:

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

Visit the following URL address:

http://serverName/hello/thinkphp

The output information is:

Routing information:
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
      
//Scheduling information:
array (size=2)
  'type' => string 'module' (length=6)
  'module' => 
    array (size=3)
      0 => null
      1 => string 'index' (length=5)
      2 => string 'hello' (length=5)
      

Set request information

If the request information obtained in some environments is wrong, you can manually set these information parameters, using the following methods:

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

Topics: PHP