Skip to content

Instantly share code, notes, and snippets.

@tranch
Last active May 9, 2016 17:15
Show Gist options
  • Select an option

  • Save tranch/a556afec665e7b5648a7d26acdc9c926 to your computer and use it in GitHub Desktop.

Select an option

Save tranch/a556afec665e7b5648a7d26acdc9c926 to your computer and use it in GitHub Desktop.
A simple router for PHP built-in server.
<?php
route('GET', '/', function ($data) {
echo 'Welcome!';
});
return false;
function route($method, $uri, $callback) {
$REQUEST_URI = explode('?', $_SERVER["REQUEST_URI"])[0];
if ($uri == $REQUEST_URI && $method == $_SERVER['REQUEST_METHOD']) {
call_user_func($callback, $_REQUEST);
exit;
}
}
@tranch
Copy link
Author

tranch commented May 9, 2016

php -S localhost:8080 router.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment