Home  >  Article  >  PHP Framework  >  Detailed explanation of how ThinkPHP receives request parameters

Detailed explanation of how ThinkPHP receives request parameters

PHPz
PHPzOriginal
2023-04-11 10:32:392267browse

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, 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');

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, 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');

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.

When accessing the

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

/user/1/John
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');
}
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!

Statement:
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