Home  >  Article  >  PHP Framework  >  How to pass GET parameters in thinkphp

How to pass GET parameters in thinkphp

PHPz
PHPzOriginal
2023-04-07 09:28:03653browse

ThinkPHP is a lightweight PHP development framework that is widely used in many PHP projects. In some projects, we need to use the GET method to pass parameters, and in ThinkPHP, using the GET method to pass parameters is also very simple.

First of all, in ThinkPHP, we can pass parameters through URL addresses. The parameters in the URL address will be automatically parsed by the ThinkPHP framework and passed to the corresponding controllers and methods.

For example, our URL address is: http://localhost/index.php/Index/index?id=1&name=thinkphp, where id=1 and name=thinkphp are the parameters passed. In the controller, we can use the $this->request->param() method to get the parameters passed in the URL address. For example:

public function index()
{
    $id = $this->request->param('id');
    $name = $this->request->param('name');
    echo 'ID=' . $id . ', Name=' . $name;
}

In this way, when we access the above URL address, the controller will output: ID=1, Name=thinkphp.

In addition to passing parameters through URL addresses, we can also use forms to pass parameters. In the HTML form, we can use the name attribute to identify the parameters that need to be passed, and in the controller, we can also use the $this->request->param() method to get the parameters passed in the form.

For example, in an HTML form, we need to pass the id and name parameters. Then you can write HTML code like this:

<form action="/index.php/Index/index" method="get">
    <input type="text" name="id" value="1">
    <input type="text" name="name" value="thinkphp">
    <input type="submit" value="提交">
</form>

In the controller, we can also use the $this->request->param() method to get the parameters passed in the form. For example:

public function index()
{
    $id = $this->request->param('id');
    $name = $this->request->param('name');
    echo 'ID=' . $id . ', Name=' . $name;
}

In this way, when we submit the form, the controller will also output: ID=1, Name=thinkphp.

In summary, GET parameters can be passed through URL addresses or forms in ThinkPHP, and can be easily obtained through the $this->request->param() method. GET parameters.

The above is the detailed content of How to pass GET parameters in thinkphp. 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