A simple classless PHP regex router function
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
RewriteEngine On | |
RewriteCond %{SCRIPT_FILENAME} !-d | |
RewriteCond %{SCRIPT_FILENAME} !-f | |
RewriteRule ^(.*)$ ./router.php/$1 [QSA] |
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 | |
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