Http.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Qii\Response;
  3. class Http extends \Qii\Base\Response
  4. {
  5. protected $_sendheader = true;
  6. protected $_responseCode = 200;
  7. /**
  8. * Set HTTP response code to use with headers
  9. *
  10. * @param int $code
  11. * @return Qii_Response_Http
  12. */
  13. public function setResponseCode($code)
  14. {
  15. if (!is_int($code) || (100 > $code) || (599 < $code)) {
  16. throw new \Qii\Exceptions\Response('Invalid HTTP response code');
  17. }
  18. $this->_responseCode = $code;
  19. return $this;
  20. }
  21. /**
  22. * Retrieve HTTP response code
  23. *
  24. * @return int
  25. */
  26. public function getResponseCode()
  27. {
  28. return $this->_responseCode;
  29. }
  30. /**
  31. * Send all headers
  32. *
  33. * Sends any headers specified.
  34. * If an {@link setResponseCode() HTTP response code}
  35. * has been specified, it is sent with the first header.
  36. *
  37. * @return Qii_Response_Http
  38. */
  39. protected function sendHeaders()
  40. {
  41. $httpCodeSent = false;
  42. foreach ($this->_headers as $header) {
  43. if (!$httpCodeSent && $this->_responseCode) {
  44. header(
  45. $header['name'] . ': ' . $header['value'],
  46. $header['replace'], $this->_responseCode
  47. );
  48. $httpCodeSent = true;
  49. } else {
  50. header(
  51. $header['name'] . ': ' . $header['value'],
  52. $header['replace']
  53. );
  54. }
  55. }
  56. return $this;
  57. }
  58. /**
  59. * Set redirect URL
  60. *
  61. * Sets Location header. Forces replacement of any prior redirects.
  62. *
  63. * @param string $url
  64. * @return Qii_Response_Abstract
  65. */
  66. public function setRedirect($url)
  67. {
  68. $this->setHeader('Location', $url, true)
  69. ->setResponseCode(302);
  70. return $this;
  71. }
  72. }