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

How to get GET and POST request parameters in ThinkPHP

Apr 11, 2023 pm 03:10 PM

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

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)