Development

#5042 (InvalidArgumentException from routing.yml in case of having a)

You must first sign up to be able to contribute.

Ticket #5042 (closed defect: fixed)

Opened 7 months ago

Last modified 7 months ago

InvalidArgumentException from routing.yml in case of having a

Reported by: n_i_o@arcor.de Assigned to: fabien
Priority: minor Milestone: 1.2.0 RC2
Component: routing Version: 1.2.0 BETA2
Keywords: Cc:
Qualification: Unreviewed

Description

Hello,

I'm switching my project from sf1.1 to sf1.2 (beta2/rc1 for now) and I'm having trouble with some default values for the route variables:

Imagine that this is one part of my routing.yml:

route_name:
  url:   /mypath/:myvariable
  param: {module: mymodule, action: myaction, myvariable: 0}

When it comes to generating an URL (p.e. with link_to in a template), I get the following error message, in case I do not specify myvariable or myvariable takes the value 0:

The "/mypath/:myvariable.html" route has some missing mandatory parameters (:myvariable).

Digging into it I found that in sfRoute.class.php (around line 208) it says

// all params must be given
if ($diff = array_diff_key($this->variables, array_filter($tparams)))
{
  throw new InvalidArgumentException(sprintf('The "%s" route has some missing mandatory parameters (%s).', $this->pattern, implode(', ', $diff)));
}

The problem here seems to be the use of array_filter() because it removes keys from the $tparams array that have a value of 0 (== false). This leads to array_diff_key to be true and throwing of the InvalidArgumentException?.

For me removing array_filter from the if-statement solved the problem.

(The use of array_filter might be incorrect in other places (in sfRoute.class.php)?!)

Change History

11/23/08 13:05:35 changed by FabianLange

  • milestone set to 1.2.0 RC2.

11/23/08 15:58:50 changed by FabianLange

a valid workaround might be

array_diff($array, array_filter($array, 'is_null'))  

which only filters out real "null"s

11/23/08 18:49:02 changed by FabianLange

i tried to remove the array_filter but there is a test that hen fails:

$route = new sfRoute('/:foo');
try
{
  $route->generate(array('foo' => ''));
  $t->fail('->generate() cannot generate a route if a variable is empty');
}
catch (Exception $e)
{
  $t->pass('->generate() cannot generate a route if a variable is empty');
}

its a bit tricky. it seems like a valid statement. however i think we should be able to put anything to the url

0 and false
null

means that the param is not present what should be allowed, even in generate url. stilll missing is

''

which is exactly covered by the test. for me it looks sensible to say: well empty string is not allowed, everything else is ok.

Proposal: remove array_filter remove testcase

11/24/08 23:22:35 changed by fabien

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

in r13322

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.