Skip to content

Commit 96c245c

Browse files
committed
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.
1 parent 25ec51d commit 96c245c

File tree

2 files changed

+110
-46
lines changed

2 files changed

+110
-46
lines changed

cake/libs/controller/controller.php

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Controller extends Object {
7474
*
7575
* Example: var $uses = array('Product', 'Post', 'Comment');
7676
*
77-
* Can be set to array() to use no models. Can be set to false to
77+
* Can be set to array() to use no models. Can be set to false to
7878
* use no models and prevent the merging of $uses with AppController
7979
*
8080
* @var mixed A single name as a string or a list of names as an array.
@@ -349,6 +349,16 @@ class Controller extends Object {
349349
*/
350350
var $validationErrors = null;
351351

352+
/**
353+
* Contains a list of the HTTP codes that CakePHP recognizes. These may be
354+
* queried and/or modified through Controller::httpCodes(), which is also
355+
* tasked with their lazy-loading.
356+
*
357+
* @var array Associative array of HTTP codes and their associated messages.
358+
* @access private
359+
*/
360+
var $__httpCodes = null;
361+
352362
/**
353363
* Constructor.
354364
*
@@ -504,6 +514,62 @@ function constructClasses() {
504514
return true;
505515
}
506516

517+
/**
518+
* Queries & sets valid HTTP response codes & messages.
519+
*
520+
* @param mixed $code If $code is an integer, then the corresponding code/message is
521+
* returned if it exists, null if it does not exist. If $code is an array,
522+
* then the 'code' and 'message' keys of each nested array are added to the default
523+
* HTTP codes. Example:
524+
*
525+
* httpCodes(404); // returns array(404 => 'Not Found')
526+
*
527+
* httpCodes(array(
528+
* 701 => 'Unicorn Moved',
529+
* 800 => 'Unexpected Minotaur'
530+
* )); // sets these new values, and returns true
531+
*
532+
* @return mixed Associative array of the HTTP codes as keys, and the message
533+
* strings as values, or null of the given $code does not exist.
534+
*/
535+
function httpCodes($code = null) {
536+
if (empty($this->__httpCodes)) {
537+
$this->__httpCodes = array(
538+
100 => 'Continue', 101 => 'Switching Protocols',
539+
200 => 'OK', 201 => 'Created', 202 => 'Accepted',
540+
203 => 'Non-Authoritative Information', 204 => 'No Content',
541+
205 => 'Reset Content', 206 => 'Partial Content',
542+
300 => 'Multiple Choices', 301 => 'Moved Permanently',
543+
302 => 'Found', 303 => 'See Other',
544+
304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',
545+
400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required',
546+
403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed',
547+
406 => 'Not Acceptable', 407 => 'Proxy Authentication Required',
548+
408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone',
549+
411 => 'Length Required', 412 => 'Precondition Failed',
550+
413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large',
551+
415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable',
552+
417 => 'Expectation Failed', 500 => 'Internal Server Error',
553+
501 => 'Not Implemented', 502 => 'Bad Gateway',
554+
503 => 'Service Unavailable', 504 => 'Gateway Time-out'
555+
);
556+
}
557+
558+
if (empty($code)) {
559+
return $this->__httpCodes;
560+
}
561+
562+
if (is_array($code)) {
563+
$this->__httpCodes = $code + $this->__httpCodes;
564+
return true;
565+
}
566+
567+
if (!isset($this->__httpCodes[$code])) {
568+
return null;
569+
}
570+
return array($code => $this->__httpCodes[$code]);
571+
}
572+
507573
/**
508574
* Loads and instantiates models required by this controller.
509575
* If Controller::persistModel; is true, controller will cache model instances on first request,
@@ -603,47 +669,8 @@ function redirect($url, $status = null, $exit = true) {
603669
}
604670

605671
if (!empty($status)) {
606-
$codes = array(
607-
100 => 'Continue',
608-
101 => 'Switching Protocols',
609-
200 => 'OK',
610-
201 => 'Created',
611-
202 => 'Accepted',
612-
203 => 'Non-Authoritative Information',
613-
204 => 'No Content',
614-
205 => 'Reset Content',
615-
206 => 'Partial Content',
616-
300 => 'Multiple Choices',
617-
301 => 'Moved Permanently',
618-
302 => 'Found',
619-
303 => 'See Other',
620-
304 => 'Not Modified',
621-
305 => 'Use Proxy',
622-
307 => 'Temporary Redirect',
623-
400 => 'Bad Request',
624-
401 => 'Unauthorized',
625-
402 => 'Payment Required',
626-
403 => 'Forbidden',
627-
404 => 'Not Found',
628-
405 => 'Method Not Allowed',
629-
406 => 'Not Acceptable',
630-
407 => 'Proxy Authentication Required',
631-
408 => 'Request Time-out',
632-
409 => 'Conflict',
633-
410 => 'Gone',
634-
411 => 'Length Required',
635-
412 => 'Precondition Failed',
636-
413 => 'Request Entity Too Large',
637-
414 => 'Request-URI Too Large',
638-
415 => 'Unsupported Media Type',
639-
416 => 'Requested range not satisfiable',
640-
417 => 'Expectation Failed',
641-
500 => 'Internal Server Error',
642-
501 => 'Not Implemented',
643-
502 => 'Bad Gateway',
644-
503 => 'Service Unavailable',
645-
504 => 'Gateway Time-out'
646-
);
672+
$codes = $this->httpCodes();
673+
647674
if (is_string($status)) {
648675
$codes = array_flip($codes);
649676
}
@@ -657,14 +684,13 @@ function redirect($url, $status = null, $exit = true) {
657684
$msg = $status;
658685
}
659686
$status = "HTTP/1.1 {$code} {$msg}";
687+
660688
} else {
661689
$status = null;
662690
}
663-
}
664-
665-
if (!empty($status)) {
666691
$this->header($status);
667692
}
693+
668694
if ($url !== null) {
669695
$this->header('Location: ' . Router::url($url, true));
670696
}

cake/tests/cases/libs/controller/controller.test.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ function testPaginate() {
563563
$this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
564564
$this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
565565
$this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
566-
566+
567567
$Controller->passedArgs = array();
568568
$Controller->paginate = array('limit' => 'garbage!');
569569
$Controller->paginate('ControllerPost');
@@ -1243,5 +1243,43 @@ function testRequestHandlerPrefers(){
12431243
$this->assertEqual($Controller->RequestHandler->prefers(), 'rss');
12441244
unset($Controller);
12451245
}
1246+
1247+
/**
1248+
* testControllerHttpCodes method
1249+
*
1250+
* @access public
1251+
* @return void
1252+
*/
1253+
function testControllerHttpCodes() {
1254+
$Controller =& new Controller();
1255+
$result = $Controller->httpCodes();
1256+
$this->assertEqual(count($result), 39);
1257+
1258+
$result = $Controller->httpCodes(100);
1259+
$expected = array(100 => 'Continue');
1260+
$this->assertEqual($result, $expected);
1261+
1262+
$codes = array(
1263+
1337 => 'Undefined Unicorn',
1264+
1729 => 'Hardy-Ramanujan Located'
1265+
);
1266+
1267+
$result = $Controller->httpCodes($codes);
1268+
$this->assertTrue($result);
1269+
$this->assertEqual(count($Controller->httpCodes()), 41);
1270+
1271+
$result = $Controller->httpCodes(1337);
1272+
$expected = array(1337 => 'Undefined Unicorn');
1273+
$this->assertEqual($result, $expected);
1274+
1275+
$codes = array(404 => 'Sorry Bro');
1276+
$result = $Controller->httpCodes($codes);
1277+
$this->assertTrue($result);
1278+
$this->assertEqual(count($Controller->httpCodes()), 41);
1279+
1280+
$result = $Controller->httpCodes(404);
1281+
$expected = array(404 => 'Sorry Bro');
1282+
$this->assertEqual($result, $expected);
1283+
}
12461284
}
12471285
?>

0 commit comments

Comments
 (0)