function test_header_status_string_works_with_many_status() { $this->assertEquals("HTTP/1.1 200 OK", No2_HTTP::header_status_string(200)); $this->assertEquals("HTTP/1.1 303 See Other", No2_HTTP::header_status_string(303)); $this->assertEquals("HTTP/1.1 403 Forbidden", No2_HTTP::header_status_string(403)); $this->assertEquals("HTTP/1.1 404 Not Found", No2_HTTP::header_status_string(404)); $this->assertEquals("HTTP/1.1 500 Internal Server Error", No2_HTTP::header_status_string(500)); }
<?php /* * some expected error status code have a more userfriendly message than the raw HTTP reason * phrase. */ $status = $this->status(); switch ($status) { case No2_HTTP::NOT_FOUND: $message = t('404 Page not found.'); break; case No2_HTTP::UNAUTHORIZED: $message = t('You are not authorized to access this page.'); break; default: $http_status_string = No2_HTTP::header_status_string($status); if (!is_null($http_status_string)) { $message = preg_replace('/HTTP\\/\\d+\\.\\d+\\s*/', '', $http_status_string); } else { $message = "An error occured."; } } ?> <h1><?php print t('Oups!'); ?> </h1> <p><?php print h($message); ?>
No2_Logger::err(__FILE__ . ': ' . $_->getMessage()); header('HTTP/1.1 500 Internal Server Error'); ?> <html> <head><title>500 Internal Server Error</title></head> <body> <h1>500 Internal Server Error</h1> <p>Please contact the site administrator</p> </body> </html> <?php die; } } $view = $controller->view(); if (No2_HTTP::is_error($view->status()) && !$controller->can_render_errors()) { /* * The controller declined error handling, so we load the default error * controller to generate the response. */ require_once APPDIR . '/controllers/error.class.php'; $controller = new ErrorController($view->status()); unset($view); goto invoke_it; } /* from this point, $controller and $view are set and valid. */ /* * Here we know the status code, log the request and render the requested ressource. */ No2_Logger::info("{$_SERVER['REMOTE_ADDR']} - {$_SERVER['REQUEST_METHOD']} - {$_SERVER['REQUEST_URI']} - {$view->status()}"); /* kindly ask the view to render the response */
/** * redirect to the given location. * * * This method is designed to be called from the controller. * * @param $location * An URI string, the location to redirect to. * * @param $status * The HTTP status code. It should be a redirect status code. The default * is 303 See Other as this method is intended to be used by action * methods modifying the database by POST requests. * (see http://en.wikipedia.org/wiki/HTTP_303) */ public function redirect_to($location, $status = No2_HTTP::SEE_OTHER) { if (No2_HTTP::is_redirection($status)) { $this->set_status($status); } $this->__redirect_location = $location; // prepare the flash for the next call. if (session_active()) { $_SESSION['_no2_flash'] = $this->flash; } }
/** * Invoke a controller action. * * This method is responsible to call the proper action method asked. It * will: * - call before_filter() to check if this controller allow the action in * the current context * - call the action. * - Forward the status code returned by the action to the view. * * @return Nothing. */ public function invoke() { $status = $this->before_filter(); if (No2_HTTP::is_success($status)) { $method = $this->respond_to($this->action, $this->http_method); $status = $this->{$method}(); } if (!is_null($status)) { $this->view->set_status($status); } }