This section describes the friendliness of thinkPHP5.0 framework after API optimization.

Source: Internet
Author: User
This article mainly introduces the friendliness of thinkPHP5.0 framework after API optimization, and analyzes the friendliness of thinkPHP5.0 framework in terms of data output and error debugging after API optimization based on specific examples, for more information about the friendliness of thinkPHP5.0 framework API after Optimization, based on the specific examples, the thinkPHP5.0 framework is analyzed to improve the friendliness of data output and error debugging after API optimization. For more information, see

This article describes the friendliness of thinkPHP5.0 framework API after optimization. We will share this with you for your reference. The details are as follows:

The new ThinkPHP version has made many optimizations for API development and does not rely on the original API mode extension.

Data output

New controller outputResponseClass, instead of directly outputting data in the controller.default_return_typeOr dynamically set different typesResponseThe output can automatically convert the data. Generally, you only need to return strings or arrays in the controller. for example, if we configure:

'default_return_type'=>'json'

The return values of the following controller method are automatically converted to json format and returned.

Namespace app \ index \ controller; class Index {public function index () {$ data = ['name' => 'thinkphp', 'URL' => 'thinkphp1. cn ']; return ['data' => $ data, 'code' => 1, 'message' => 'Operation completed'];}

After accessing the request URL, you can see the following output in the browser:

The code is as follows:

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

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

You can specify the output type explicitly. for example, you can specify the JSON data output as follows:

Namespace app \ index \ controller; class Index {public function index () {$ data = ['name' => 'thinkphp', 'URL' => 'thinkphp1. cn ']; // return json (['data' => $ data, 'code' => 1, 'message' => 'Operation completed ']);}

Or specify the output XML data type:

Namespace app \ index \ controller; class Index {public function index () {$ data = ['name' => 'thinkphp', 'URL' => 'thinkphp1. cn ']; // specify the return xml (['data' => $ data, 'code' => 1, 'message' => 'Operation completed ']);}

Data types supported by the core include view, xml, json, and jsonp. for other types, you need to expand them by yourself.

Error debugging

Because API development is not convenient for development and debugging on the client, the Trace debugging function of ThinkPHP5 supports Socket and remote development and debugging.

Setting method:

'App _ track' => true, 'track' => ['type' => 'socket ', // socket server 'host' => 'slog .thinkphp1.cn',],

After installing the chrome browser plug-in, you can perform remote debugging. For more information, see the debugging section.

The above section details the friendly analysis after thinkPHP5.0 framework API optimization. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.