Skip to content

Instantly share code, notes, and snippets.

@iiidefix
Last active June 18, 2023 16:25
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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