Skip to content

Instantly share code, notes, and snippets.

@binarypie
Created August 13, 2009 23:13
Show Gist options
  • Save binarypie/167512 to your computer and use it in GitHub Desktop.
Save binarypie/167512 to your computer and use it in GitHub Desktop.
try {
Routes::map(array({
'/do/some/(?P<thing>\w+)' => 'Thing::do',
}));
} catch (PathNotFoundException $e) {
echo '<p>404 Error:</p><p>'.$e->getMessage().'</p>Allowed Urls:<hr />';
foreach ($urls as $path => $function) {
echo $path.' => '.$function.'<br />';
}
} catch (Exception $e) {
echo '<p>500 Error:</p><p>'.$e->getMessage().'</p>Stack:<hr />';
foreach ($e->getTrace() as $stackItem) {
$lineMessage = '';
foreach ($stackItem as $text) {
$lineMessage = $lineMessage.' '.$text;
}
echo $lineMessage.'<br />';
}
}
class Thing {
public static function do($request) {
$restParam = $request->rest_param('thing');
$getParam = $request->get_param('foo');
$postParam = $request->post_param('bar');
$cookieValue = $request->cookie_param('foobar');
}
}
/**
* Typed Exception for 404 errors
*
* @param String $message A message describing the error
*/
class PathNotFoundException extends \Exception {}
class Request {
private $method = null;
private $restParams = array();
private $getParams = array();
private $postParams = array();
private $cookieParams = array();
function __construct($method, $restParams) {
$this->method = $method;
$this->restParams = array_merge($this->restParams, $restParams);
$this->getParams = array_merge($this->getParams, $_GET);
$this->postParams = array_merge($this->postParams, $_POST);
$this->cookiearams = array_merge($this->cookieParams, $_COOKIE);
}
public function rest_param($name) {
return array_key_exists($name,$this->restParams) ? urldecode($this->restParams[$name]) : null;
}
public function get_param($name) {
return array_key_exists($name,$this->getParams) ? urldecode($this->getParams[$name]) : null;
}
public function post_param($name) {
return array_key_exists($name,$this->postParams) ? urldecode($this->postParams[$name]) : null;
}
public function cookie_param($name) {
return array_key_exists($name,$this->cookieParams) ? urldecode($this->cookieParams[$name]) : null;
}
public function method() {
return $this->method;
}
}
class Routes {
/**
* Maps a regex-based url to a function
*
* @param array $urls The regex-based url to function mapping
* @throws PathNotFoundException Thrown if no match is found
* @throws Exception Thrown if function is not callable
*/
static function map($urls) {
$method = strtoupper($_SERVER['REQUEST_METHOD']);
$path = $_SERVER['REQUEST_URI'];
$found = false;
krsort($urls);
foreach ($urls as $regex => $function) {
$regex = str_replace('/', '\/', $regex);
$regex = '^' . $regex . '\/?(\?.*)?$';
if (preg_match("/$regex/i", $path, $matches)) {
$found = true;
if (is_callable($function)) {
call_user_func($function, new Request($method, $matches));
} else {
throw new \Exception('Function, '.$function.', does not exist or contains errors.');
}
break;
}
}
if (!$found) {
throw new PathNotFoundException('Path Not Found: '.$path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment