Four ways to obtain data uploaded by ThinkPHP6 form [request object call, static call, helper function call, native get|post]

Data acquisition of ThinkPHP6 form upload

First, the Input control in a form needs to have a corresponding name value, then the method is post|get, and the action is the method of the controller to be submitted

For example (the front-end view code of this article is the following html):

<form method="post" action="/index.php/login/login" >
    <input type="text" name="username"><br>
    <input type="text" name="password"><br>
    <input type="submit" value="提交">
</form>

This form means to submit data to the login method under the login.php controller

The request parameters in the form Requestcan be obtained through , or can be obtained using native $_GET or $_POST, and then roughly divided into request object calls, Facade calls, and helper function calls.

Then we will verify these three methods with a small example:

1. Request object call

There are two types of request object calls, constructor injection (providing a constructor and a Request instance) and operation method injection (injection through function parameters).

1.1 Constructor injection

<?php
namespace app\controller;

use app\BaseController;
use think\Request; // 系统的think\Request对象


class Login extends BaseController
{
    
    
    // request 实例
    protected $request;

    // request 构造器注入方式
    public function __construct(Request $request)
    {
    
    
        $this->request = $request;
    }


    public function login(){
    
    
        return $this->request->param('username');
    }
}

Be careful not to introduce the wrong Request here, the Request injected by the construction method needs to be think\Request.

Test it, ok, get it correctly.

insert image description here

insert image description here

1.2 Action Method Injection

<?php
namespace app\controller;

use app\BaseController;
use think\Request; // 系统的think\Request对象


class Login extends BaseController
{
    
    

    // 在方法参数上,将Request注入
    public function login(Request $request){
    
    
        return $request->param('username');
    }
}

insert image description here

insert image description here

2. Static call (Facade)

In occasions where dependency injection is not used, Facadea mechanism can be used to statically call the method of the request object (note usethe difference between the introduced class libraries).

Request object calls use use think\Request; , while static calls use use think\facade\Request;.

<?php
namespace app\controller;

use app\BaseController;
use think\facade\Request; 


class Login extends BaseController
{
    
    

    
    public function login(){
    
    
        // 静态调用
        return Request::param('username');
    }
}

3. Helper function call

In order to simplify calls, the system also provides requesthelper functions that can directly call the current request object whenever needed.

<?php
namespace app\controller;

use app\BaseController;

class Login extends BaseController
{
    
    

    public function login(){
    
    
        // 助手函数调用
        return request()->param('username');
    }
}

Helper function calls do not require the use of additional libraries.

4. Native GET and POST

Get the parameters of the request form through the original $_GETand .$_POST

<?php
namespace app\controller;

use app\BaseController;

class Login extends BaseController
{
    
    

    public function login(){
    
    
        return $_POST['username'];
    }
}

or

<?php
namespace app\controller;

use app\BaseController;

class Login extends BaseController
{
    
    

    public function login(){
    
    
        return $_GET['username'];
    }
}

summary

In tp6, there are many ways to obtain the uploaded form parameters, including request object call, static call, helper function call, and native $_GETones $_POST.

But it should be noted that when using the request object to call, the imported Requestshould be think\Request. RequestIt should be introduced when calling statically think\facade\Request. $_GETHelper function calls and native sums $_POSTdon't need to be imported Request.

Finally, the action field of the form request needs to be written as /entry file/controller name/method (/index.php/login/login)


Guess you like

Origin blog.csdn.net/m0_63622279/article/details/130714826