Home  >  Article  >  PHP Framework  >  How to get GET and POST request parameters in ThinkPHP

How to get GET and POST request parameters in ThinkPHP

PHPz
PHPzOriginal
2023-04-11 15:10:072549browse

ThinkPHP is an open source PHP development framework that provides a simple, fast and efficient solution for web application development. In this framework, it is common to use GET and POST requests to pass parameters. This article will introduce how to obtain GET and POST request parameters in the ThinkPHP framework.

  1. Get GET request parameters

In ThinkPHP, use the input() function to get the parameters of the GET request. The first parameter of the input() function is the parameter name, and the second parameter is the default value. If the first parameter is not passed, all GET request parameters are obtained by default.

The sample code is as follows:

use think\facade\Request;
 
// 获取所有GET请求参数
$getParams = Request::param();
 
// 获取指定参数名称的GET请求参数
$getParam = Request::param('name');
 
// 获取指定参数名称的GET请求参数,如果没有则使用默认值
$getParamDefault = Request::param('name', 'default value');
  1. Getting POST request parameters

The way to get POST request parameters is similar to getting GET request parameters. You can also use input ()function. The difference is that you need to determine whether the current request is a POST request before obtaining the parameters.

The sample code is as follows:

use think\facade\Request;
 
// 判断当前请求是否为POST请求
if(Request::isPost()){
    // 获取所有POST请求参数
    $postParams = Request::param();
     
    // 获取指定参数名称的POST请求参数
    $postParam = Request::param('name');
     
    // 获取指定参数名称的POST请求参数,如果没有则使用默认值
    $postParamDefault = Request::param('name', 'default value');
}

In addition to using the input() function, you can also use the request() function to obtain GET and POST request parameters. The request() function supports both GET and POST requests, and can obtain other types of request parameters, such as Cookie, Session, server variables, etc.

The sample code is as follows:

use think\facade\Request;
 
// 获取所有GET和POST请求参数
$params = Request::request();
 
// 获取指定参数名称的GET和POST请求参数
$param = Request::request('name');
 
// 获取指定参数名称的GET和POST请求参数,如果没有则使用默认值
$paramDefault = Request::request('name', 'default value');

Summary

Through the introduction of this article, we can learn how to obtain GET and POST request parameters in the ThinkPHP framework. Whether using the input() function or the request() function, you can easily get the request parameters. Developers can choose the appropriate method to obtain parameters according to their own needs.

The above is the detailed content of How to get GET and POST request 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