From faf05d6894a670a209c70df4999f10a6d54c9a57 Mon Sep 17 00:00:00 2001 From: Hassan Khan Date: Fri, 31 Jan 2014 08:53:08 +0000 Subject: [PATCH] =?UTF-8?q?``Zepto\Router``=20now=20keeps=20track=20of=20t?= =?UTF-8?q?he=20current=20route=E2=80=99s=20HTTP=20status?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/Zepto/Router.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/library/Zepto/Router.php b/library/Zepto/Router.php index 35898ad..5d18bcb 100644 --- a/library/Zepto/Router.php +++ b/library/Zepto/Router.php @@ -94,6 +94,12 @@ class Router */ protected $current_route; + /** + * Current route's status code. Is either 200, 404 or 500 + * @var integer + */ + protected $current_http_status; + /** * An array containing the list of routing rules and their callback * functions, as well as their request method and any additional paramters. @@ -226,10 +232,12 @@ public function run() // Call not found handler if no match was found if ($route === null) { + $this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND; $this->not_found(); } // If route is a valid Route object, then try and execute its callback else { + // Set current route $this->current_route = $route; @@ -239,6 +247,8 @@ public function run() // Try to execute callback for route, if it fails, catch the exception // and generate a HTTP 500 error try { + $this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_OK; + // Set response content $this->response->setContent(call_user_func_array($route->callback(), array($params))); @@ -246,6 +256,7 @@ public function run() $this->response->send(); } catch (\Exception $e) { + $this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR; $this->error($e->getMessage()); } } @@ -275,6 +286,16 @@ public function current_route() return $this->current_route; } + /** + * Returns the HTTP status code of the currently matched route + * + * @return integer + */ + public function current_http_status() + { + return $this->current_http_status; + } + /** * ERROR HANDLING */ @@ -305,7 +326,7 @@ public function error($arg = null) } // Set response's status code - $this->response->setStatusCode(\Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR); + $this->response->setStatusCode($this->current_http_status); // Send response $this->response->send(); @@ -336,7 +357,7 @@ public function not_found($callback = null) $this->response->setContent(call_user_func(array($this, 'default_not_found_handler'))); } // Set response's status code - $this->response->setStatusCode(\Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND); + $this->response->setStatusCode($this->current_http_status); // Send response $this->response->send();