Skip to content

Instantly share code, notes, and snippets.

@anandkunal
Created April 28, 2011 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anandkunal/946649 to your computer and use it in GitHub Desktop.
Save anandkunal/946649 to your computer and use it in GitHub Desktop.
Some random routing idea...
<?
class ToroRoute {
function __construct($route, $patterns) {
}
public function matches_url($url) {
}
public static function prepare_route() {
}
}
class SomeHandler {
public function get($username, $action) {
echo "hi $username - you want to call $action";
}
}
/////////////////////////////////////////////
function prepare_named_routes($matches) {
return "([a-zA-Z0-9-_]+)";
}
// Have a URL
$url = "/user/dude/rug";
// Prepare the route
$route = "user/:username/:action";
$route_regex = str_replace('/', '\/', $route);
$route_regex = preg_replace_callback("|:[a-zA-Z_]+|", "prepare_named_routes", $route_regex);
// Match the route to the url
if (preg_match('/^\/' . $route_regex . '\/?$/', $url, $matches)) {
$handler_instance = new SomeHandler();
$request_method = "get";
unset($matches[0]);
$method_arguments = $matches;
call_user_func_array(array($handler_instance, $request_method), $method_arguments);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment