Issues

ZF-2301: Zend_Controller_Router_Route_Regex::assemble() uses default even when parameter is matched

Description

It seems like default values are being used even when parameters are actually matched from the url. Take the following for example:


$route = new Zend_Controller_Router_Route_Regex(
  "itemlist(?:/(\d+))?"
, array('page' => 1) // Defaults
, array(1 => 'page') // Parameter map
, 'itemlist/%d'
);

$values = $route->match('/itemlist/2');

var_dump($values);

$url = $route->assemble();

// Expect string(10) "itemlist/2"
var_dump($url);

$url = $route->assemble(array('page' => 2));

// Expect string(10) "itemlist/2"
var_dump($url);

Which results in:

array(1) {
  ["page"]=>
  string(1) "2"
}
string(10) "itemlist/1"
string(10) "itemlist/2"

This appears to work fine if the parameter map is reversed so that 'page' is the key and '1' is the value.

Comments

Good catch, Chris. It may be tricky to get straight, though.

As you already see, ZF supports declaring parameter names in both ways. I would like to keep it that way but I'm afraid we will have to do some magic from the looks of it.

If you already have some working code, then please go ahead and share.

Hey Martel,

I think I've found the issue (it has to do with the way that assemble() merges data from defaults, values and provided data).

If this patch works for you, can I go ahead and commit?

Thanks, Ralph

Looks very good, Ralph. Go ahead and commit. Thanks.

But for the sake of my conscience, please change the last lines of the test. This:


+        // check to make sure that the assembly will return with provided page=2 in the correct place
+        $this->assertEquals('itemlist/2', $route->assemble(array('page' => 2)));

Should be changed into this:


+        // check to make sure that the assembly will return with provided page=2 in the correct place
+        $this->assertEquals('itemlist/3', $route->assemble(array('page' => 3)));
+
+        // check to make sure that the assembly can reset a single parameter
+        $this->assertEquals('itemlist/1', $route->assemble(array('page' => null)));

First to make sure we don't get a 2 out of URL values, second to make sure parameter is being reset back to default.

fixed in r8470

Keeping open until its merged into 1.5.0 branch.

FIxed in both the 1.5 branch as well as trunk