OSDN Git Service

Fixes #2 - now compatible with PHP 5.2.17, did not test any lower versions.
authorDanny van Kooten <dannyvankooten@gmail.com>
Tue, 3 Jan 2012 08:30:09 +0000 (09:30 +0100)
committerDanny van Kooten <dannyvankooten@gmail.com>
Tue, 3 Jan 2012 08:30:09 +0000 (09:30 +0100)
Router.php
example.php

index c8972f6..b83201e 100644 (file)
@@ -31,6 +31,8 @@ final class Router {
      * @var string
      */
     private $base_url = '';
+    
+    private $route_args = array();
 
     /**
      * Creates an instance of the Router class
@@ -73,7 +75,7 @@ final class Router {
      * @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;
@@ -109,17 +111,12 @@ final class Router {
         // 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))
@@ -174,6 +171,22 @@ final class Router {
         $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
index ba55ffa..e9c4d1f 100644 (file)
@@ -6,26 +6,26 @@ require 'Router.php';
 $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());
@@ -41,4 +41,4 @@ if ($r->hasRoute()) {
 
 ?><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