Skip to content

Instantly share code, notes, and snippets.

@iiidefix
Last active June 18, 2023 16:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iiidefix/ea2ba83b51045ed667eaaa40d60cba88 to your computer and use it in GitHub Desktop.
Save iiidefix/ea2ba83b51045ed667eaaa40d60cba88 to your computer and use it in GitHub Desktop.
A simple classless PHP regex router function
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./router.php/$1 [QSA]
<?php
function route($regex, $callback) {
$regex = '/^' . str_replace('/', '\/', $regex) . '$/';
if (preg_match($regex, $_SERVER['REQUEST_URI'], $params)) {
array_shift($params);
return call_user_func_array($callback, array_values($params));
}
}
route('blog/(\w+)/(\d+)', function($category, $id){
print $category . ':' . $id;
die(); //stop processing more routes
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment