在 CakePHP 中使用自定义状态代码 2.x
Use a Custom Status Code with CakePHP 2.x
我需要为 CakePHP 2.x
中的响应使用自定义状态代码,例如 422
。
然而 CakeResponse
class only allows those status codes that are already setup in the CakeResponse class.
有没有办法让我在 CakePHP
中使用我的自定义状态代码?
注意如果答案是使用自定义 CakeResponse
class there is a related question regarding that.
您查找 API 很酷,但请继续阅读:
Queries & sets valid HTTP response codes & messages.
/**
* Queries & sets valid HTTP response codes & messages.
*
* @param int|array $code If $code is an integer, then the corresponding code/message is
* returned if it exists, null if it does not exist. If $code is an array, then the
* keys are used as codes and the values as messages to add to the default HTTP
* codes. The codes must be integers greater than 99 and less than 1000. Keep in
* mind that the HTTP specification outlines that status codes begin with a digit
* between 1 and 5, which defines the class of response the client is to expect.
* Example:
*
* httpCodes(404); // returns array(404 => 'Not Found')
*
* httpCodes(array(
* 381 => 'Unicorn Moved',
* 555 => 'Unexpected Minotaur'
* )); // sets these new values, and returns true
*
* httpCodes(array(
* 0 => 'Nothing Here',
* -1 => 'Reverse Infinity',
* 12345 => 'Universal Password',
* 'Hello' => 'World'
* )); // throws an exception due to invalid codes
*
* For more on HTTP status codes see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
*
* @return mixed associative array of the HTTP codes as keys, and the message
* strings as values, or null of the given $code does not exist.
* @throws CakeException If an attempt is made to add an invalid status code
*/
如果您打算将 none 标准代码与 API 一起使用,我会 return x-header 中的代码或者像 Jsend 那样的响应中的代码例如。
我需要为 CakePHP 2.x
中的响应使用自定义状态代码,例如 422
。
然而 CakeResponse
class only allows those status codes that are already setup in the CakeResponse class.
有没有办法让我在 CakePHP
中使用我的自定义状态代码?
注意如果答案是使用自定义 CakeResponse
class there is a related question regarding that.
您查找 API 很酷,但请继续阅读:
Queries & sets valid HTTP response codes & messages.
/**
* Queries & sets valid HTTP response codes & messages.
*
* @param int|array $code If $code is an integer, then the corresponding code/message is
* returned if it exists, null if it does not exist. If $code is an array, then the
* keys are used as codes and the values as messages to add to the default HTTP
* codes. The codes must be integers greater than 99 and less than 1000. Keep in
* mind that the HTTP specification outlines that status codes begin with a digit
* between 1 and 5, which defines the class of response the client is to expect.
* Example:
*
* httpCodes(404); // returns array(404 => 'Not Found')
*
* httpCodes(array(
* 381 => 'Unicorn Moved',
* 555 => 'Unexpected Minotaur'
* )); // sets these new values, and returns true
*
* httpCodes(array(
* 0 => 'Nothing Here',
* -1 => 'Reverse Infinity',
* 12345 => 'Universal Password',
* 'Hello' => 'World'
* )); // throws an exception due to invalid codes
*
* For more on HTTP status codes see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
*
* @return mixed associative array of the HTTP codes as keys, and the message
* strings as values, or null of the given $code does not exist.
* @throws CakeException If an attempt is made to add an invalid status code
*/
如果您打算将 none 标准代码与 API 一起使用,我会 return x-header 中的代码或者像 Jsend 那样的响应中的代码例如。