OSDN Git Service

Fixes #4
authorDanny van Kooten <dannyvankooten@gmail.com>
Fri, 10 Feb 2012 09:21:59 +0000 (10:21 +0100)
committerDanny van Kooten <dannyvankooten@gmail.com>
Fri, 10 Feb 2012 09:23:33 +0000 (10:23 +0100)
- Forced suffixing of Route URL's with a forward slash.
- Added getters and setters for parameters.

README.md
Route.php
Router.php
example.php

index 4044af8..2cbf670 100644 (file)
--- a/README.md
+++ b/README.md
@@ -23,6 +23,9 @@ A simple Rails inspired PHP router class.
     // or somewhat more complicated
     $router->map('/users/:id/edit/', array('controller' => 'SomeController', 'action' => 'someAction'), array('methods' => 'GET,PUT', 'name' => 'users_edit'));
 
+    // You can even specify closures as the Route's target
+    $router->map('/hello/:name', function($name) { echo "Hello $name."; });
+
     // match current request URL & http method
     $target = $router->matchCurrentRequest();
     var_dump($target);
index 5ebd159..80af4ac 100644 (file)
--- a/Route.php
+++ b/Route.php
@@ -32,11 +32,22 @@ class Route {
        */
        private $filters = array();
 
+       /**
+       * Array containing parameters passed through request URL
+       * @var array
+       */
+       private $params = array();
+
        public function getUrl() {
                return $this->url;
        }
 
        public function setUrl($url) {
+               $url = (string) $url;
+
+               // make sure that the URL is suffixed with a forward slash
+               if(substr($url,-1) !== '/') $url .= '/';
+               
                $this->url = $url;
        }
 
@@ -80,6 +91,14 @@ class Route {
         return "(\w+)";
        }
 
+       public function getParameters() {
+               return $this->parameters;
+       }
+
+       public function setParameters(array $parameters) {
+               $this->parameters = $parameters;
+       }
+
 
 
 
index cfa3f28..d1cc6eb 100644 (file)
@@ -102,7 +102,9 @@ class Router {
 
             }
 
-            return $route->getTarget();
+            $route->setParameters($params);
+
+            return $route;
             
         }
 
index 011701b..dbac877 100644 (file)
@@ -5,15 +5,21 @@ require 'Route.php';
 $router = new Router();
 $router->setBasePath('/PHP-Router');
 
-// maps / to controller 'users' and method 'index'.
 $router->map('/', 'someController:indexAction', array('methods' => 'GET'));
 $router->map('/users/:id/edit/', 'users#edit', array('methods' => 'GET', 'name' => 'users_edit'));
 $router->map('/contact/',array('controller' => 'someController', 'action' => 'contactAction'), array('name' => 'contact'));
 $router->map('/users/','users#create', array('methods' => 'POST', 'name' => 'users_create'));
 $router->map('/users/','users#list', array('methods' => 'GET', 'name' => 'users_list'));
 
+$route = $router->matchCurrentRequest();
+
 ?><h3>Current URL & HTTP method would route to: </h3>
-<pre><?php var_dump($router->matchCurrentRequest()); ?></pre>
+<?php if($route) { ?>
+       <pre><?php var_dump($route->getTarget()); ?></pre>
+       <pre><?php var_dump($route->getParameters()); ?></pre>
+<?php } else { ?>
+       <pre>No route matched.</pre>
+<?php } ?>
 
 <h3>Try out these URL's.</h3>
 <p><a href="<?php echo $router->generate('users_edit', array('id' => 5)); ?>"><?php echo $router->generate('users_edit', array('id' => 5)); ?></a></p>