mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Moving list of http codes out of Controller::redirect() and into a new method, Controller::httpCodes() for better reusability. See doc blocks for usage details.
This commit is contained in:
parent
25ec51d3ae
commit
96c245c6a7
2 changed files with 110 additions and 46 deletions
|
@ -74,7 +74,7 @@ class Controller extends Object {
|
|||
*
|
||||
* Example: var $uses = array('Product', 'Post', 'Comment');
|
||||
*
|
||||
* Can be set to array() to use no models. Can be set to false to
|
||||
* Can be set to array() to use no models. Can be set to false to
|
||||
* use no models and prevent the merging of $uses with AppController
|
||||
*
|
||||
* @var mixed A single name as a string or a list of names as an array.
|
||||
|
@ -349,6 +349,16 @@ class Controller extends Object {
|
|||
*/
|
||||
var $validationErrors = null;
|
||||
|
||||
/**
|
||||
* Contains a list of the HTTP codes that CakePHP recognizes. These may be
|
||||
* queried and/or modified through Controller::httpCodes(), which is also
|
||||
* tasked with their lazy-loading.
|
||||
*
|
||||
* @var array Associative array of HTTP codes and their associated messages.
|
||||
* @access private
|
||||
*/
|
||||
var $__httpCodes = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
|
@ -504,6 +514,62 @@ class Controller extends Object {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries & sets valid HTTP response codes & messages.
|
||||
*
|
||||
* @param mixed $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 'code' and 'message' keys of each nested array are added to the default
|
||||
* HTTP codes. Example:
|
||||
*
|
||||
* httpCodes(404); // returns array(404 => 'Not Found')
|
||||
*
|
||||
* httpCodes(array(
|
||||
* 701 => 'Unicorn Moved',
|
||||
* 800 => 'Unexpected Minotaur'
|
||||
* )); // sets these new values, and returns true
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
function httpCodes($code = null) {
|
||||
if (empty($this->__httpCodes)) {
|
||||
$this->__httpCodes = array(
|
||||
100 => 'Continue', 101 => 'Switching Protocols',
|
||||
200 => 'OK', 201 => 'Created', 202 => 'Accepted',
|
||||
203 => 'Non-Authoritative Information', 204 => 'No Content',
|
||||
205 => 'Reset Content', 206 => 'Partial Content',
|
||||
300 => 'Multiple Choices', 301 => 'Moved Permanently',
|
||||
302 => 'Found', 303 => 'See Other',
|
||||
304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',
|
||||
400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required',
|
||||
403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed',
|
||||
406 => 'Not Acceptable', 407 => 'Proxy Authentication Required',
|
||||
408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone',
|
||||
411 => 'Length Required', 412 => 'Precondition Failed',
|
||||
413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large',
|
||||
415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable',
|
||||
417 => 'Expectation Failed', 500 => 'Internal Server Error',
|
||||
501 => 'Not Implemented', 502 => 'Bad Gateway',
|
||||
503 => 'Service Unavailable', 504 => 'Gateway Time-out'
|
||||
);
|
||||
}
|
||||
|
||||
if (empty($code)) {
|
||||
return $this->__httpCodes;
|
||||
}
|
||||
|
||||
if (is_array($code)) {
|
||||
$this->__httpCodes = $code + $this->__httpCodes;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!isset($this->__httpCodes[$code])) {
|
||||
return null;
|
||||
}
|
||||
return array($code => $this->__httpCodes[$code]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads and instantiates models required by this controller.
|
||||
* If Controller::persistModel; is true, controller will cache model instances on first request,
|
||||
|
@ -603,47 +669,8 @@ class Controller extends Object {
|
|||
}
|
||||
|
||||
if (!empty($status)) {
|
||||
$codes = array(
|
||||
100 => 'Continue',
|
||||
101 => 'Switching Protocols',
|
||||
200 => 'OK',
|
||||
201 => 'Created',
|
||||
202 => 'Accepted',
|
||||
203 => 'Non-Authoritative Information',
|
||||
204 => 'No Content',
|
||||
205 => 'Reset Content',
|
||||
206 => 'Partial Content',
|
||||
300 => 'Multiple Choices',
|
||||
301 => 'Moved Permanently',
|
||||
302 => 'Found',
|
||||
303 => 'See Other',
|
||||
304 => 'Not Modified',
|
||||
305 => 'Use Proxy',
|
||||
307 => 'Temporary Redirect',
|
||||
400 => 'Bad Request',
|
||||
401 => 'Unauthorized',
|
||||
402 => 'Payment Required',
|
||||
403 => 'Forbidden',
|
||||
404 => 'Not Found',
|
||||
405 => 'Method Not Allowed',
|
||||
406 => 'Not Acceptable',
|
||||
407 => 'Proxy Authentication Required',
|
||||
408 => 'Request Time-out',
|
||||
409 => 'Conflict',
|
||||
410 => 'Gone',
|
||||
411 => 'Length Required',
|
||||
412 => 'Precondition Failed',
|
||||
413 => 'Request Entity Too Large',
|
||||
414 => 'Request-URI Too Large',
|
||||
415 => 'Unsupported Media Type',
|
||||
416 => 'Requested range not satisfiable',
|
||||
417 => 'Expectation Failed',
|
||||
500 => 'Internal Server Error',
|
||||
501 => 'Not Implemented',
|
||||
502 => 'Bad Gateway',
|
||||
503 => 'Service Unavailable',
|
||||
504 => 'Gateway Time-out'
|
||||
);
|
||||
$codes = $this->httpCodes();
|
||||
|
||||
if (is_string($status)) {
|
||||
$codes = array_flip($codes);
|
||||
}
|
||||
|
@ -657,14 +684,13 @@ class Controller extends Object {
|
|||
$msg = $status;
|
||||
}
|
||||
$status = "HTTP/1.1 {$code} {$msg}";
|
||||
|
||||
} else {
|
||||
$status = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($status)) {
|
||||
$this->header($status);
|
||||
}
|
||||
|
||||
if ($url !== null) {
|
||||
$this->header('Location: ' . Router::url($url, true));
|
||||
}
|
||||
|
|
|
@ -563,7 +563,7 @@ class ControllerTest extends CakeTestCase {
|
|||
$this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
|
||||
$this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
|
||||
$this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
|
||||
|
||||
|
||||
$Controller->passedArgs = array();
|
||||
$Controller->paginate = array('limit' => 'garbage!');
|
||||
$Controller->paginate('ControllerPost');
|
||||
|
@ -1243,5 +1243,43 @@ class ControllerTest extends CakeTestCase {
|
|||
$this->assertEqual($Controller->RequestHandler->prefers(), 'rss');
|
||||
unset($Controller);
|
||||
}
|
||||
|
||||
/**
|
||||
* testControllerHttpCodes method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testControllerHttpCodes() {
|
||||
$Controller =& new Controller();
|
||||
$result = $Controller->httpCodes();
|
||||
$this->assertEqual(count($result), 39);
|
||||
|
||||
$result = $Controller->httpCodes(100);
|
||||
$expected = array(100 => 'Continue');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$codes = array(
|
||||
1337 => 'Undefined Unicorn',
|
||||
1729 => 'Hardy-Ramanujan Located'
|
||||
);
|
||||
|
||||
$result = $Controller->httpCodes($codes);
|
||||
$this->assertTrue($result);
|
||||
$this->assertEqual(count($Controller->httpCodes()), 41);
|
||||
|
||||
$result = $Controller->httpCodes(1337);
|
||||
$expected = array(1337 => 'Undefined Unicorn');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$codes = array(404 => 'Sorry Bro');
|
||||
$result = $Controller->httpCodes($codes);
|
||||
$this->assertTrue($result);
|
||||
$this->assertEqual(count($Controller->httpCodes()), 41);
|
||||
|
||||
$result = $Controller->httpCodes(404);
|
||||
$expected = array(404 => 'Sorry Bro');
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Reference in a new issue