/** * Analyze controller return value and send appropriate headers such as 404, 302, 301, redirect to internal routes. * * <p>It is very SEO friendly but you would need to know the basics of HTTP status code.</p> * <p>Automatically handles 404, include error document or redirect to inner route * to handle the error based on config <b>ERROR_404_DOCUMENT</b> and <b>ERROR_404_ROUTE</b></p> * <p>Controller return value examples:</p> * <code> * 404 #send 404 header * array('/internal/route', 404) #send 404 header & redirect to an internal route * 'http://www.google.com' #redirect to URL. default 302 Found sent * array('http://www.google.com',301) #redirect to URL. forced 301 Moved Permenantly sent * array('/hello/sayhi', 'internal') #redirect internally, 200 OK * </code> * @param mixed $code */ public function throwHeader($code) { if (headers_sent()) { return; } if ($code != null) { if (is_int($code)) { if ($code === 404) { //Controller return 404, send 404 header, include file if ERROR_404_DOCUMENT is set by user header('HTTP/1.1 404 Not Found'); if (!empty(Doo::conf()->ERROR_404_DOCUMENT)) { include Doo::conf()->SITE_PATH . Doo::conf()->ERROR_404_DOCUMENT; } elseif (!empty(Doo::conf()->ERROR_404_ROUTE)) { $this->reroute(Doo::conf()->ERROR_404_ROUTE, true); } exit; } else { DooUriRouter::redirect(null, true, $code); } } elseif (is_string($code)) { //Controller return the redirect location, it sends 302 Found DooUriRouter::redirect($code); } elseif (is_array($code)) { //Controller return array('/some/routes/here', 'internal') if ($code[1] == 'internal') { $this->reroute($code[0]); exit; } elseif ($code[1] === 404) { $this->reroute($code[0], true); exit; } elseif ($code[1] === 302) { DooUriRouter::redirect($code[0], true, $code[1], array("HTTP/1.1 302 Moved Temporarily")); } else { DooUriRouter::redirect($code[0], true, $code[1]); } } } }
public function throwHeader($code) { if (headers_sent()) { return; } if ($code != NULL) { if (is_int($code)) { if ($code === 404) { header('HTTP/1.1 404 Not Found'); if (!empty(Doo::conf()->ERROR_404_DOCUMENT)) { include Doo::conf()->SITE_PATH . Doo::conf()->ERROR_404_DOCUMENT; } elseif (!empty(Doo::conf()->ERROR_404_ROUTE)) { $this->reroute(Doo::conf()->ERROR_404_ROUTE, true); } exit; } else { DooUriRouter::redirect(NULL, true, $code); } } elseif (is_string($code)) { DooUriRouter::redirect($code); } elseif (is_array($code)) { if ($code[1] == 'internal') { $this->reroute($code[0]); exit; } elseif ($code[1] === 404) { $this->reroute($code[0], true); exit; } elseif ($code[1] === 302) { DooUriRouter::redirect($code[0], true, $code[1], array("HTTP/1.1 302 Moved Temporarily")); } else { DooUriRouter::redirect($code[0], true, $code[1]); } } } }