[limb-svn] r7133 - in 3.x/trunk/limb/web_app: src/request tests/cases/plain/request
svn at limb-project.com
svn at limb-project.com
Tue Jul 29 16:46:15 MSD 2008
Author: conf
Date: 2008-07-29 16:46:15 +0400 (Tue, 29 Jul 2008)
New Revision: 7133
URL: http://fisheye.limb-project.com/changelog/limb/?cs=7133
Modified:
3.x/trunk/limb/web_app/src/request/lmbRoutes.class.php
3.x/trunk/limb/web_app/tests/cases/plain/request/lmbRoutesToUrlTest.class.php
Log:
-- fixed adding unneeded params to url in lmbRoutes::toUrl, urls become more clean
-- fixed tests
Modified: 3.x/trunk/limb/web_app/src/request/lmbRoutes.class.php
===================================================================
--- 3.x/trunk/limb/web_app/src/request/lmbRoutes.class.php 2008-07-29 09:57:06 UTC (rev 7132)
+++ 3.x/trunk/limb/web_app/src/request/lmbRoutes.class.php 2008-07-29 12:46:15 UTC (rev 7133)
@@ -192,6 +192,11 @@
{
if(strpos($path, ':'.$param_name) === false)
continue;
+
+ if (isset($route['defaults'][$param_name]) && ($route['defaults'][$param_name] === $param_value)) {
+ unset($params[$param_name]); // default params will be substituted lower
+ continue;
+ }
$path = str_replace(':'. $param_name, $param_value, $path);
unset($params[$param_name]);
@@ -202,8 +207,20 @@
if(isset($route['defaults']))
{
+ // we define here required default params for building right url,
+ // other params at the end of the path can be omitted.
+ if (preg_match_all('|(:\w+/?)+(?=/\w+)|', $path, $required_params))
+ $required_params= $required_params[0];
+
foreach($route['defaults'] as $param_name => $param_value)
- $path = str_replace(':'. $param_name, $param_value, $path);
+ {
+ if(!in_array(':' . $param_name, $required_params))
+ $param_value = '';
+
+ $path = str_replace(':' . $param_name, $param_value, $path);
+ }
+
+ $path = preg_replace('~/+~', '/', $path);
}
if(strpos($path, "/:") !== false)
Modified: 3.x/trunk/limb/web_app/tests/cases/plain/request/lmbRoutesToUrlTest.class.php
===================================================================
--- 3.x/trunk/limb/web_app/tests/cases/plain/request/lmbRoutesToUrlTest.class.php 2008-07-29 09:57:06 UTC (rev 7132)
+++ 3.x/trunk/limb/web_app/tests/cases/plain/request/lmbRoutesToUrlTest.class.php 2008-07-29 12:46:15 UTC (rev 7133)
@@ -50,7 +50,7 @@
'defaults' => array('action' => 'display')));
$routes = new lmbRoutes($config);
- $this->assertEqual($routes->toUrl(array('controller' => 'news'), 'default'), '/news/display');
+ $this->assertEqual($routes->toUrl(array('controller' => 'news'), 'default'), '/news/');
}
function testThrowExceptionIfNotEnoughParams()
@@ -136,5 +136,30 @@
{
$path = str_replace('/admin_', '/admin/', $path);
}
+
+ function testRemoveUnneededDefaultParamsFromUrl()
+ {
+ $config = array(
+ 'default' => array(
+ 'path' => '/users/:user/:controller/:action/:id/',
+ 'defaults' => array(
+ 'user' => 'admin',
+ 'controller' => 'blog',
+ 'action' => 'display',
+ 'id' => 0
+ )
+ )
+ );
+
+ $routes = new lmbRoutes($config);
+
+ $this->assertEqual($routes->toUrl(array()), '/users/');
+ $this->assertEqual($routes->toUrl(array('user' => 'bob')), '/users/bob/');
+ $this->assertEqual($routes->toUrl(array('user' => 'admin')), '/users/');
+ $this->assertEqual($routes->toUrl(array('user' => 'bob', 'action' => 'index')), '/users/bob/blog/index/');
+ $this->assertEqual($routes->toUrl(array('controller' => 'article')), '/users/admin/article/');
+ $this->assertEqual($routes->toUrl(array('controller' => 'article', 'id' => 5)), '/users/admin/article/display/5/');
+ $this->assertEqual($routes->toUrl(array('user' => 'admin', 'action' => 'display', 'id' => 0)), '/users/');
+ }
}
More information about the limb-svn
mailing list