Home > PHP Framework > ThinkPHP > body text

Detailed explanation of how ThinkPHP receives request parameters

PHPz
Release: 2023-04-11 14:17:08
Original
2266 people have browsed it

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'];
Copy after login

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');
Copy after login

Among them, getIndicates 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');
Copy after login

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'];
Copy after login

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');
Copy after login

Among them, postIndicates 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');
Copy after login

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');
Copy after login

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');
Copy after login

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)
{
    // ...
}
Copy after login

The above code indicates that a ## is defined The #user method receives two dynamic parameters $id and $name.

When accessing the

user method, you can use the following URL to access:

/user/1/John
Copy after login
The above URL indicates that

id=1 and are passed name=JohnTwo parameters.

In the controller, you can use the following code to receive parameters:

public function user($id,$name)
{
    $id = $this->request->param('id');
    $name = $this->request->param('name');
}
Copy after login
The above content is a detailed introduction to the method of receiving request parameters in ThinkPHP.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template