summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoman Tsiupa <draconyster@gmail.com>2013-02-26 22:30:07 +0200
committerRoman Tsiupa <draconyster@gmail.com>2013-02-26 22:30:07 +0200
commit5401ac6ec6da5ad70c5ca73753447cca859699a2 (patch)
tree2dbbcf6f0f7644cda2adcfb26b1dbd9d13bd2910
parent1690da36a5c5ad309de7117f3d8b52f045ebc9bf (diff)
downloadPHPixie-5401ac6ec6da5ad70c5ca73753447cca859699a2.zip
PHPixie-5401ac6ec6da5ad70c5ca73753447cca859699a2.tar.gz
PHPixie-5401ac6ec6da5ad70c5ca73753447cca859699a2.tar.bz2
Support for optional parameters
-rw-r--r--system/classes/route.php10
-rw-r--r--tests/system/routeTest.php5
2 files changed, 11 insertions, 4 deletions
diff --git a/system/classes/route.php b/system/classes/route.php
index c1b9457..244a0c7 100644
--- a/system/classes/route.php
+++ b/system/classes/route.php
@@ -80,13 +80,19 @@ class Route {
$url = is_array($this->rule)?$this->rule[0]:$this->rule;
- $replace = array('(' => '', ')' => '');
+ $replace = array();
$params = array_merge($this->defaults, $params);
foreach($params as $key => $value)
$replace["<{$key}>"] = $value;
-
$url = str_replace(array_keys($replace), array_values($replace), $url);
+ $count = 1;
+ $chars='[^\(\)]*?';
+ while($count>0)
+ $url = preg_replace("#\({$chars}<{$chars}>{$chars}\)#", '', $url, -1, $count);
+
+ $url = str_replace(array('(', ')'), '', $url);
+
if ($absolute)
$url = $protocol.'://'.$_SERVER['HTTP_HOST'].$url;
diff --git a/tests/system/routeTest.php b/tests/system/routeTest.php
index 58d0295..dc22931 100644
--- a/tests/system/routeTest.php
+++ b/tests/system/routeTest.php
@@ -113,11 +113,12 @@ class RoteTest extends PHPUnit_Framework_TestCase
*/
public function testUrl()
{
- Route::add('url', '/<controller>/<action>(/<id>)', array(
+ Route::add('url', '(/<controller>(/<action>(/<id>)))', array(
'controller' => 'home',
'action' => 'index'
));
$route = Route::get('url');
- $this->assertEquals('/home/index/5', $route->url(array('id'=>5)));
+ $this->assertEquals('/home/index/5', $route-> url(array('id' => 5)));
+ $this->assertEquals('/home/index', $route->url());
}
}