* @var string
*/
private $base_url = '';
+
+ private $route_args = array();
/**
* Creates an instance of the Router class
* @return boolean True if route matches URL, false if not.
*/
public function match($route_url, $target = '', array $args = array()) {
-
+
// check if this is a named route, if so, store it.
if (isset($args['as'])) {
$this->named_routes[$args['as']] = $route_url;
// check for matching URL
$request_url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$request_url = rtrim($request_url, '/');
-
+
+ // store route filter arguments as instance variable so callback function can access them later on
+ $this->route_args = $args;
+
// setup route regex for route url
- $route_regex = preg_replace_callback("/:(\w+)/", function($matches) use ($args) {
-
- // does match have filter regex set?
- if (isset($args['filters']) && isset($matches[1]) && isset($args['filters'][$matches[1]])) {
- return $args['filters'][$matches[1]];
- }
-
- return "(\w+)";
- }, $this->base_url . $route_url);
+ $route_regex = preg_replace_callback("/:(\w+)/", array(&$this, 'setup_filter_regex'), $this->base_url . $route_url);
// check if request url matches route regex. if not, return false.
if (!preg_match("@^{$route_regex}*$@i", $request_url, $matches))
$this->route = array('controller' => $controller, 'action' => $action, 'params' => $params);
return true;
}
+
+ /**
+ * Used as a callback in preg_replace_callback to substitute default regex with custom regex specified by the 'filter' argument.
+ *
+ * @param array $matches
+ * @return string
+ */
+ private function setup_filter_regex($matches) {
+
+ // does match have filter regex set?
+ if (isset($this->route_args['filters']) && isset($matches[1]) && isset($this->route_args['filters'][$matches[1]])) {
+ return $this->route_args['filters'][$matches[1]];
+ }
+
+ return "(\w+)";
+ }
/**
* Matches REST URL's for a given controller
$r = new Router('/rest-router');
// maps / to controller 'users' and method 'index'.
-$r->match('/', 'users#index');
+#$r->match('/', 'users#index');
// maps /user/5 to controller 'users', method 'show' with parameter 'id' => 5
// this route won't match /users/i5 because of the filter regex.
$r->match('/users/:id', 'users#show', array('filters' => array('id' => '(\d+)')));
// maps POST request to /users/ to controller 'users' and method 'create'
-$r->match('/users', 'users#create', array('via' => 'post'));
+#$r->match('/users', 'users#create', array('via' => 'post'));
// maps /photos/show to controller 'photos' and method 'show'
-$r->match('/photos/show');
+#$r->match('/photos/show');
// maps GET /users/5/edit to controller 'users', method 'edit' with parameters 'id' => 5 and saves route as a named route.
-$r->match('/users/:id/edit', 'users#edit', array('via' => 'get', 'as' => 'user_edit_page'));
+#$r->match('/users/:id/edit', 'users#edit', array('via' => 'get', 'as' => 'user_edit_page'));
// maps multiple routes
// GET /users will map to users#index
// GET /users/5 will map to users#show
-$r->resources('users', array('only' => 'index,show'));
+#$r->resources('users', array('only' => 'index,show'));
if ($r->hasRoute()) {
extract($r->getRoute());
?><h3>Reversed routing</h3><?php
// echoes /users/5/edit
-echo "Route for user_edit_page with ID 5: ". $r->reverse('user_edit_page', array('id' => '5'));
\ No newline at end of file
+#echo "Route for user_edit_page with ID 5: ". $r->reverse('user_edit_page', array('id' => '5'));
\ No newline at end of file