@@ -74,7 +74,7 @@ class Controller extends Object {
74
74
*
75
75
* Example: var $uses = array('Product', 'Post', 'Comment');
76
76
*
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
78
78
* use no models and prevent the merging of $uses with AppController
79
79
*
80
80
* @var mixed A single name as a string or a list of names as an array.
@@ -349,6 +349,16 @@ class Controller extends Object {
349
349
*/
350
350
var $ validationErrors = null ;
351
351
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
+
352
362
/**
353
363
* Constructor.
354
364
*
@@ -504,6 +514,62 @@ function constructClasses() {
504
514
return true ;
505
515
}
506
516
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
+
507
573
/**
508
574
* Loads and instantiates models required by this controller.
509
575
* If Controller::persistModel; is true, controller will cache model instances on first request,
@@ -603,47 +669,8 @@ function redirect($url, $status = null, $exit = true) {
603
669
}
604
670
605
671
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
+
647
674
if (is_string ($ status )) {
648
675
$ codes = array_flip ($ codes );
649
676
}
@@ -657,14 +684,13 @@ function redirect($url, $status = null, $exit = true) {
657
684
$ msg = $ status ;
658
685
}
659
686
$ status = "HTTP/1.1 {$ code } {$ msg }" ;
687
+
660
688
} else {
661
689
$ status = null ;
662
690
}
663
- }
664
-
665
- if (!empty ($ status )) {
666
691
$ this ->header ($ status );
667
692
}
693
+
668
694
if ($ url !== null ) {
669
695
$ this ->header ('Location: ' . Router::url ($ url , true ));
670
696
}
0 commit comments