Requirements: a request using the postman api interface to process the data output on some unusual circumstances we hope that through their writing some classes and methods
Easy to implement back-end processing personnel to debug the prompts! When the test set app_debug => true
-------------------------------------------------------------------------------------------------------------------------------------------
Create a test interface as an example: position in application \ api \ controller \ Test.php (can not find the location to create a new folder!)
The test interface to create the corresponding routing location: application \ route.php
If I save the request in the test method of the interface need to receive information returned by the self-organization, such as I
public function save(){
return [
'status'=>1,
'message'=>'OK',
'data'=>input('post.')
];
}
Such postman receive information returned by a long way
This is very troublesome so we can encapsulate a common method call when to pass parameters to
We package a function to position a public function file common.php this file in application \ common.php
/ **
* universal API interface data output
* @param int $ status service status code
* @param string $ message information presentation
* @param [] $ data Data
* @param int $ httpCode http status code
* @return Array
* /
function Show ($ Status, $ Message, $ Data = [], $ httpCode = 200 is) {
$ Data = [
'Status' => $ Status,
'Message' => $ Message,
'Data' => $ Data,
] ;
return JSON (Data $, $ httpCode);
}
After packaging can be called at the above-mentioned methods
public function save(){
return show(1,'OK',input('post.'),200);
}
And return the result is the same as above
Custom render method this method exists in thinkphp \ library \ think \ exception \ Handle.php
we can write ApiHandleException.php
location of the file in the application \ common \ lib \ exception \ ApiHandleException.php ( directly under the new common directory lib folder)
related comments of the code written in the code
? < PHP namespace App \ the Common \ lib \ Exception; use Exception; use of Think \ Exception \ the Handle; / ** * Class ApiHandleException * @ abnormal internal data output solutions for the render * / class ApiHandleException the extends the Handle { // status code public $ = 500 httpCode; // modify config.php rewriting arranged subsequent to the preparation method of exception_handle class inheritance appears System Error error or the like arranged before // public function the render (Exception $ E) { // If the judgment is on debug mode then role is to allow the server personnel is able to know the specific location of the error iF (config ( 'app_debug') == to true) { return parent :: the render ($ E); } // determines whether an object is an instance of a class if ($ e instanceof ApiException) { $this->httpCode = $e->httpCode;//将ApiException的code传过来 } return show(0,$e->getMessage(),[],$this->httpCode); } }
After writing exception_handle we also need to modify the value of the config.php file
config.php file location in the application \ config.php
Configure its default value is empty we fill in the content after found exception_handle
// use the exception handling empty handle class \ Think \ Exception \ the Handle
'exception_handle' => '\ App \ Common \ lib \ Exception \ ApiHandleException',
Can test
public function save(){
if($data['ids']){
echo 111;
exit;
}
}
The request may be cleared to know the specific location of the error
Custom exception method
First, we tested with built-in method
the Save function public () {
$ the Data = the INPUT ( "POST.");
// Let mt there is no
IF ($ the Data [ 'mt'] = 1!) {
Exception ( 'data you submit is illegal!');
}
}
The information returned is
But this is not a fixed state on the basis of code so we can render the above-mentioned method on a new class and method definitions from
Create a file ApiException.php
Location: application \ common \ lib \ exception \ ApiException.php
<?php namespace app\common\lib\exception; use think\Exception; use Throwable; //自定义 exception方法 class ApiException extends Exception{ public $message = ''; public $httpCode = 500; public $code = 0; public function __construct($message = '',$httpCode = 0 ,$code = 0) { $this->message = $message; $this->httpCode = $httpCode; $this->code = $code; } }
When we write we have just rewrite method content
the Save function public () {
$ the Data = the INPUT ( "POST.");
// Let mt does not exist
IF (! $ the Data [ 'mt'] = 1) {
// rewritten
throw new ApiException ( 'you submit data illegal ', 400);!
}
}
Making request to see the information returned, you can see different results
About this abnormal data output TP5 resolved internally on here
Disclaimer: This article is the original article CSDN bloggers "Zhang Shibayama", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/zhangzeshan/article/details/91882300