Development

#2317 (Bug in sfRouting::generate() with $params having numeric keys)

You must first sign up to be able to contribute.

Ticket #2317 (closed defect: fixed)

Opened 1 year ago

Last modified 1 year ago

Bug in sfRouting::generate() with $params having numeric keys

Reported by: ghuytro Assigned to: fabien
Priority: minor Milestone: 1.0.9
Component: routing Version: 1.0.0
Keywords: routing generate url parameters Cc:
Qualification: Unreviewed

Description

When using numeric parameter names for the $params array, the sfRouting::generate() method produces odd results

Assume routing rule as follows:

show_results:
  url: /results/*
  param: { module: results, action: index }

OK - Using String Parameter Names

$name = 'show_results';
$params = array(
   'name' => 'Bob',
   'city' => 'Timbuktu'
);
$url = sfRouting::getInstance()->generate( $name, $params, $querydiv = '/', $divider = '/', $equals = '/' );

Returns:

/results/name/Bob/city/Timbuktu

FAIL - Using Numeric Parameter Names

$name = 'show_results';
$params = array(
   '15' => 'Bob',
   '76' => 'Timbuktu'
);
$url = sfRouting::getInstance()->generate( $name, $params, $querydiv = '/', $divider = '/', $equals = '/' );

Returns:

/results/0/Bob/1/Timbuktu

instead of the expected

/results/15/Bob/76/Timbuktu

Change History

10/03/07 14:51:59 changed by ghuytro

  • status changed from new to closed.
  • resolution set to fixed.

I've been able to track down the problem.

In sfRouting.class.php, on line 458 you have:

457 	
458 	    $params = array_merge($defaults, $params);
459 	

As per the documentation at http://www.php.net/array_merge, numeric keys are renumbered when merging arrays - as demonstrated in the example I posted in the previous message.

It is recommended in the php.net documentation for array_merge to merge arrays when you have numeric keys as follows:

$newArray = $firstArray + $secondArray;

Thus, the fix for this bug is to change line 458 in sfRouting.class.php as follows:

457 	
458 	    $params = $defaults + $params;
459 	

I have tested this fix and it works.

10/03/07 16:09:32 changed by fabien

  • status changed from closed to reopened.
  • resolution deleted.

10/12/07 18:06:40 changed by noel

  • milestone set to 1.0.9.

11/21/07 10:16:21 changed by noel

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [6125]) sfRouting : allow numeric parameters (closes #2317)