There are many ways to obtain parameters in ThinkPHP6, and there are also many tips for using them. Many novices often know one method, but later they find the second and third methods when looking at other people's codes, and they are confused...
Let’s take a look at the many ways to obtain parameters in ThinkPHP6.
Let us first assume that there is the following url:
A:http://www.a.com/index/index/hello/id/1.html
B:http://www.a.com/index/index/hello?id=1
C:http://www.a.com/index/index/hello?name=12aa
D:http://www.a.com/index/index/hello?name=aa123dd
First type: dependency injection
dump($this->request->param());//All parameters, return array
array:1 [ "name" => "1" ]
dump($this->request->param('id'));/ /Specific parameters, return string
dump($this->request->get('id'));//Only valid for type B URLs
dump($this ->request->param('name','aaa'));//If no name parameter is passed, set the default value and return the string aaa
dump($this->request- >param('name','1','intval'));Convert the received parameters to integer type. Type A URL returns the default value 1, type C URL returns 12, and type D URL returns 0
Second: Use the helper function
var_dump(input('id'));//Both Class A url and Class B url return string 1. If Class C url and Class D url are not passed, NULL will be returned
The third method: static acquisition
Introduce use think\facade\Request;
before useCheck whether the variable is set:
Request::has('id','get');Both class A url and class B url return true, class C url and class D url return false
Request::has('name','post'); Check whether there is a posted name and return true or false
Request::param('name');// Get The name variable of the current request, returns a string, does not pass the output null
Request::param(); // Get all variables of the current request (filtered)
Request::param( false);//Get all unfiltered variables of the current request
Request::param(['name', 'email']);//Get some variables
Finally:
In addition, in some cases, we also need to determine what the request is,
For example:
if($request->isPost()){
//判断是否是post请求
}Similar situations include $ request->isGet(), $request->isAjax().
The above are the various ways to obtain parameters in ThinkPHP6. It may not be comprehensive, but mastering these can basically satisfy the parameter acquisition in most situations...
The above is the detailed content of 3 common methods for obtaining parameters in ThinkPHP6 [Summary]. For more information, please follow other related articles on the PHP Chinese website!
What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PMThe article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.
How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PMArticle discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.
What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PMThe article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges
How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PMThe article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]
What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PMThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159
How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PMThe article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.
What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PMThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.
How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PMThe article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools






