Home > PHP Framework > ThinkPHP > body text

3 common methods for obtaining parameters in ThinkPHP6 [Summary]

王雪芹
Release: 2020-04-30 16:03:04
Original
4288 people have browsed it

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"
 ]
Copy after login

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 use

Check 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请求
}
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template