Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Zepto\Router now keeps track of the current route’s HTTP status
Browse files Browse the repository at this point in the history
  • Loading branch information
hassankhan committed Jan 31, 2014
1 parent 0d08256 commit faf05d6
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions library/Zepto/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand All @@ -239,13 +247,16 @@ 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)));

// Send response
$this->response->send();
}
catch (\Exception $e) {
$this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR;
$this->error($e->getMessage());
}
}
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit faf05d6

Please sign in to comment.