ThinkPHP is a PHP open source framework based on MVC architecture. There are many ways to pass parameters. Here are some commonly used methods.
For example: http://localhost/index.php/Home/Index/index?id=1
It can be obtained in the controller like this: $id = $_GET[ 'id'];
For example: there is an input box in the form whose name attribute is age. It can be obtained in the controller like this: $age = $_POST['age'];
For example: Generate route in the controller:
$url = url('index/details', ['id' => 1] );
Access in routing:
public function details($id)
{
echo $id;
}
For example: Assign a value in a page: session('username', 'tom');
Can be obtained in the controller like this: $username = session(' username');
For example: assign a value in a page: cookie('username', 'tom', 3600);
It can be obtained in the controller like this: $username = cookie('username');
This is a more flexible way method, it supports both GET and POST methods.
For example: $id = request()->param('id');
Summary
These are relatively common methods of passing parameters, allowing developers to More flexible and convenient use in projects. In project development, we need to flexibly choose the appropriate way to transfer parameters based on the actual situation.
The above is the detailed content of How to pass parameters in thinkphp. For more information, please follow other related articles on the PHP Chinese website!