Skip to content
Browse files

updating to allow for named params in routes (see README)

  • Loading branch information...
1 parent 66ec7ab commit bf5ae8c8fb2923b58ea9f0f727c6dc212b29e8c2 @enygma committed Nov 2, 2012
Showing with 59 additions and 4 deletions.
  1. +16 −1 README.md
  2. +42 −2 Shield/Shield.php
  3. +1 −1 Shield/View.php
View
17 README.md
@@ -123,7 +123,22 @@ $app->get('/foo([0-9]+)', function($matches){
});
```
-You would get `Array ( [0] => /foo123 [1] => 123 )` in the `$matches` variable.
+You would get `Array ( [1] => 123 )` in the `$matches` variable.
+
+You can also use named parameters in your routes too:
+
+```php
+include_once '../Shield/Shield.php';
+$app = new Shield\Shield();
+
+$app->get('/params/[:t1]/[:t2]', function($matches){
+ return $app->view->render('First Parameter: '.$matches['t1']);
+});
+```
+
+If your route is `/params/123/456` you'll get:
+
+`Array ( [t1] => 123, [t2] => 456 )` in the `$matches` variable.
*NOTE:* DO NOT directly use the values from this array - there is currently no filtering on these values
so there is potential for exploitation.
View
44 Shield/Shield.php
@@ -11,6 +11,13 @@ class Shield
private $routes = array();
/**
+ * Contains any paramaters passed in the URL
+ * Ex.: /param/[:t1]/[:t2]
+ * @var array
+ */
+ private $params = array();
+
+ /**
* Logging object (Shield\Log)
* @var object
*/
@@ -105,8 +112,21 @@ public function __call($func, $args)
}
if (isset($args[1])) {
+
+ // see if the path includes params
+ if (strpos($path, ':') !== false) {
+ preg_match_all('/\[(.+?)\]/', $path, $params);
+
+ // and replace the values in the path
+ foreach ($params[1] as $p) {
+ $path = str_replace('/['.$p.']','/(.+?)', $path);
+ }
+ $this->params[$func][$path] = $params[1];
+ }
+
$this->routes[$func][$path] = $args[1];
- $this->log->log('SETTING PATH ['.strtoupper($func).']: '.$path);
+
+ $this->log->log('SETTING PATH ['.strtoupper($func).']: '.$path);
} else {
$this->throwError('No path to set for : '.strtoupper($func));
$this->log->log('NO PATH TO SET ['.strtoupper($func).']: '.$path);
@@ -165,9 +185,12 @@ public function run()
$found = false;
if (isset($this->routes[$method])) {
+
// loop through our routes and see if there's a regex match
foreach ($this->routes[$method] as $route => $handler) {
if (preg_match('#^'.$route.'$#', $uri, $matches) === 1 && $found == false) {
+ // drop the first value, it's the full URL
+ $matches = array_values(array_slice($matches,1));
$found = true;
$this->routeMatch($method, $route, $matches);
}
@@ -202,8 +225,25 @@ private function routeMatch($method,$uri,$matches=null)
// route match!
$this->log->log('ROUTE MATCH ['.strtoupper($method).']: '.$uri);
- $routeClosure = $this->routes[$method][$uri]($matches);
+ //see if we have params to match the items to
+ if ($matches !== null && isset($this->params[$method][$uri])) {
+ $params = $this->params[$method][$uri];
+
+ // match them up
+ $tmp = array();
+ foreach ($matches as $index => $match) {
+ if (isset($params[$index])) {
+ $param = str_replace(':','',$params[$index]);
+ $tmp[$param] = $match;
+ }
+ }
+ if (!empty($tmp)){
+ $matches = $tmp;
+ }
+ }
+
+ $routeClosure = $this->routes[$method][$uri]($matches);
$content = $this->view->render($routeClosure);
echo $content;
}
View
2 Shield/View.php
@@ -206,7 +206,7 @@ public function render($template)
// scope in the tempate extraction
$result = function($file,array $data=array()) {
ob_start();
- extract($this->_properties);
+ extract($data);
try {
include $file;
} catch(\Exception $e) {

0 comments on commit bf5ae8c

Please sign in to comment.
Something went wrong with that request. Please try again.