Home > Backend Development > PHP Tutorial > Detailed introduction to the friendliness analysis of thinkPHP5.0 framework API after optimization

Detailed introduction to the friendliness analysis of thinkPHP5.0 framework API after optimization

黄舟
Release: 2023-03-06 17:56:02
Original
1264 people have browsed it

This article mainly introduces the friendliness of the thinkPHP5.0 frameworkAPI after optimization, and analyzes the data output and errors after API optimization of the thinkPHP5.0 framework with specific examples. For improvements in debugging friendliness, friends in need can refer to

. This article describes the friendliness of the thinkPHP5.0 framework API after optimization. Share it with everyone for your reference, the details are as follows:

The new version of ThinkPHP has made a lot of optimizations for API development and does not rely on the original API mode expansion.

Data output

The new version of ControllerThe output is processed uniformly using the Response class instead of directly in the controller For output, data conversion processing can be automatically performed by setting default_return_type or dynamically setting different types of Response output. Generally speaking, you only need to return a string or array in the controller. That's it. For example, if we configure:

'default_return_type'=>'json'
Copy after login

, then the return value of the following controller method will be automatically converted to json format and returned.

namespace app\index\controller;
class Index
{
  public function index()
  {
    $data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
    return ['data'=>$data,'code'=>1,'message'=>'操作完成'];
  }
}
Copy after login

After accessing the request URL address, you can finally see the output in the browser as follows:

Copy code The code is as follows:

{"data":{"name":"thinkphp","url":"thinkphp.cn"},"code":1,"message":"\u64cd\u4f5c\u5b8c\u6210"}
Copy after login

If you need to return other data formats, the code of the controller itself does not need to be changed.

Supports output by explicitly specifying the output type. For example, specifying JSON data output as follows:

namespace app\index\controller;
class Index
{
  public function index()
  {
    $data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
    // 指定json数据输出
    return json(['data'=>$data,'code'=>1,'message'=>'操作完成']);
  }
}
Copy after login

or specifying output of XML type data:

namespace app\index\controller;
class Index
{
  public function index()
  {
    $data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
    // 指定xml数据输出
    return xml(['data'=>$data,'code'=>1,'message'=>'操作完成']);
  }
}
Copy after login

Core supported data types include view, xml, json and jsonp. Other types need to be extended by yourself.

Error debugging

Due to API development, it is inconvenient to develop and debug on the client, but the Trace debugging function of ThinkPHP5 supports methods including Socket, which can achieve remote development. debug.

Setting method:

'app_trace' => true,
'trace'   => [
  'type'       => 'socket',
  // socket服务器
  'host'       => 'slog.thinkphp.cn',
],
Copy after login
Then install the chrome browser plug-in to perform

remote debugging, please refer to the debugging section for details.

The above is the detailed content of Detailed introduction to the friendliness analysis of thinkPHP5.0 framework API after optimization. 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