Home>Article>PHP Framework> Three methods of obtaining parameters in ThinkPHP6
The following tutorial column ofthinkphp frameworkwill introduce to you three methods of obtaining parameters in ThinkPHP6. I hope it will be helpful to friends in need!
##There are many ways to get parameters in ThinkPHP6, and there are There are many tips to use. Many novices often know one method. Later, when looking at other people's code, they find the second and third methods, and they are confused... Let's take a look at ThinkPHP6 There are many ways to get parameters. 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 arrayarray:1 [ "name" => "1" ]dump($this->request->param('id'));//Specific parameters , returns the string dump($this->request->get('id'));//Only valid for type B url 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 returnedThe 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 variablesFinally:
In addition, in some cases, we also need to determine what the request is, For example:if($request->isPost()){ //判断是否是post请求}Similar situations are
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 Three methods of obtaining parameters in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!