SoFunction
Updated on 2025-03-09

Analysis of the request method and response method of Thinkphp5.0 framework

This article describes the request method and response method of the Thinkphp5.0 framework. Share it for your reference, as follows:

Thinkphp5.0 request method

Method 1 (using the helper function provided by the framework):

public function index(){
  $request = request();
  dump($request);
}

Method 2 (using the Request class under think to get the instance):

use think\Request;
public function index(){
  $request = Request::instance();
  dump($request);
}

Method 3 (using the Request class under think, the method of injecting objects):

use think\Request;
class Index{
  public function index(Request $request){
    dump($request);
  }
}

Commonly used request methods:

//Browser address informationdump($request->domain());
dump($request->path());
dump($request->url());
//Request methoddump($request->method());
dump($request->isGet());
dump($request->isPost());
dump($request->isAjax());
//Get parametersdump($request->get());
dump($request->post());
dump($request->param());
//A specific parameterdump($request->get('id'));
//Get session and cookies//session('username','zhang san');
dump($request->session());
dump($request->cookie());
//Get the current module, controller, operationdump($request->module());
dump($request->controller());
dump($request->action());

input() helper function

//Use of input helper function//Input() takes $request->param() method by default//The default is the get method if the method is not specifieddump(input('id'));
dump(input(''));
dump($request->get('id'));
dump(input('',100));//The second parameter is the default valuedump($request->post('id',100));

How to respond to Thinkphp5.0

$res = config('default_return_type');
dump($res);//Default is html//Modify to json\think\Config::set('default_return_type','json');
$res = config('default_return_type');
dump($res);//json
$data = ['code'=>200,'result'=>['id'=>1,'name'=>'aa']];
return $data;

For more information about thinkPHP related content, please check out the topic of this site:ThinkPHP Introduction Tutorial》、《Summary of thinkPHP template operation skills》、《Summary of common methods of ThinkPHP》、《Codeigniter Introductory Tutorial》、《Advanced tutorial on CI (CodeIgniter) framework》、《Zend FrameWork Framework Introduction Tutorial"and"PHP template technical summary》。

I hope that the description in this article will be helpful to everyone's PHP programming based on the ThinkPHP framework.