Response Class
WeeSpeed uses the Response class to encapsulate methods related to responses. This class is only for outputting responses, so all its methods are static. Here's how to use it:
Response::success('Execution successful');
Response::error(400001, 'No access rights');Methods
The Response class encapsulates the following methods:
success($data)
Output a successful response in JSON format. The response structure is as follows:
| Field Name | Type | Description | Example |
|---|---|---|---|
| code | int | Status code | 0 |
| msg | string | Status description | ok |
| data | any | Response data |
error($code, $msg='',$data = null)
输出一个 json 格式的错误响应 ,响应结构为:
| Field Name | Type | Description | Example |
|---|---|---|---|
| code | int | Status code | 400001 |
| msg | string | Status description | No access rights |
| data | any | Response data | Data for front - end debugging |
Error Codes
WeeSpeed has built - in common error codes, which are defined in the framework/libraries/ErrorType.phpp file to simplify the response of error messages. For example:
Response::error(ErrorType::MISSING_PARAMETER, "Name");当使用内置错误码时,对应的错误消息提示会添加到 $msg 前面,上面的语句输出为:
{
"code": 10006,
"msg": "Missing required parameter: Name"
}redirect($url)
Redirect to the specified URL.
rawOutput($httpCode, $data = null)
Directly output an HTTP response. The parameters are as follows:
| Field Name | Type | Description | Example |
|---|---|---|---|
| httpCode | int | HTTP status code | 200 |
| data | any | Response data |
提示
The methods of the response class are already encapsulated in the base controller class. You can use $this->to execute these methods in the controller.