Created
April 28, 2011 16:04
-
-
Save anandkunal/946649 to your computer and use it in GitHub Desktop.
Some random routing idea...
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
<? | |
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