Skip to content

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:

php
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 NameTypeDescriptionExample
codeintStatus code0
msgstringStatus descriptionok
dataanyResponse data

error($code, $msg='',$data = null)

输出一个 json 格式的错误响应 ,响应结构为:

Field NameTypeDescriptionExample
codeintStatus code400001
msgstringStatus descriptionNo access rights
dataanyResponse dataData 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:

php
Response::error(ErrorType::MISSING_PARAMETER, "Name");

当使用内置错误码时,对应的错误消息提示会添加到 $msg 前面,上面的语句输出为:

json
{
  "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 NameTypeDescriptionExample
httpCodeintHTTP status code200
dataanyResponse 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.