Skip to content

Instantly share code, notes, and snippets.

@mrjazz
Last active June 7, 2016 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrjazz/d993e3986ba099e8b9c3926abd39cfd7 to your computer and use it in GitHub Desktop.
Save mrjazz/d993e3986ba099e8b9c3926abd39cfd7 to your computer and use it in GitHub Desktop.
<?php
// endpoints
function getToken() {
echo 'GET token';
}
function postToken() {
echo 'POST token';
}
//routing
router([
'GET:auth/token' => 'getToken',
'POST:auth/token' => 'postToken'
]);
// framework
function router($routes) {
foreach ($routes as $rule => $func) {
$parts = explode(':', $rule);
if (
count($parts) == 2
&& $_SERVER['REQUEST_METHOD'] == $parts[0]
&& preg_match('/' . addcslashes($parts[1], '/') . '/i', $_SERVER['REQUEST_URI'])
) {
if (function_exists($func)) {
return $func();
} else {
throw new Exception("Function $func doesn't exists", 1);
}
}
}
echo 'Endpoint "' . $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . '" not found';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment