ThinkPHP is a very popular PHP framework, and its request parameter reception is very flexible and convenient. This article will introduce in detail how to receive request parameters in ThinkPHP.
1. Receiving GET request parameters
1.1 Direct reading
The simplest way to receive GET request parameters is to read the parameters directly, as follows:
$id = $_GET['id'];
Among them, id
is the name of the request parameter.
1.2 Using the input assistant function
ThinkPHP provides the input assistant function, which can also be used to receive GET request parameters, as follows:
$id = input('get.id');
Among them, get
Indicates the request method used, id
is the name of the request parameter.
1.3 Using I assistant function
ThinkPHP also provides I assistant function, which can also be used to receive GET request parameters, as follows:
$id = I('get.id');
Among them, get
indicates the request method used, id
is the name of the request parameter.
2. Receiving POST request parameters
2.1 Direct reading
The simplest way to receive POST request parameters is to read the parameters directly, as follows:
$name = $_POST['name'];
Among them, name
is the name of the request parameter.
2.2 Using the input assistant function
ThinkPHP provides the input assistant function, which can also be used to receive POST request parameters, as follows:
$name = input('post.name');
Among them, post
Indicates the request method used, name
is the name of the request parameter.
2.3 Using the I assistant function
ThinkPHP also provides the I assistant function, which can also be used to receive POST request parameters, as follows:
$name = I('post.name');
Among them, post
indicates the request method used, name
is the name of the request parameter.
3. Receiving routing parameters
In ThinkPHP, routing parameters can also be received as request parameters, which is very convenient to use. As follows:
Route::get('user/:id', 'user/read');
The above code indicates that a route named user/read
is defined and a parameter named id
is received.
In the controller, you can use the following code to receive parameters:
$id = $this->request->param('id');
Among them, param
indicates receiving parameters, and id
is the parameter name.
4. Receiving dynamic parameters
In ThinkPHP, you can use dynamic parameters to receive parameters, as follows:
public function user($id,$name) { // ... }
The above code indicates that a ## is defined The #user method receives two dynamic parameters
$id and
$name.
user method, you can use the following URL to access:
/user/1/John
id=1 and
are passed name=JohnTwo parameters.
public function user($id,$name) { $id = $this->request->param('id'); $name = $this->request->param('name'); }
The above is the detailed content of Detailed explanation of how ThinkPHP receives request parameters. For more information, please follow other related articles on the PHP Chinese website!