Last active
June 7, 2016 10:31
-
-
Save mrjazz/d993e3986ba099e8b9c3926abd39cfd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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