2
votes

probably this is a quick one, but I can't get my head around right now.

I have the following problem using unnamed optional parameters. Let's say I have the following a action in a controller with two fixed and two optional parameters:

function action ($param1, $param2, $param3=NULL, $param4=NULL) {
//any code
}

$param3 and $param4 can exist both, none or one of the two. Everything works fine if either none, all four or the first three parameters are set. e.g.

www.domain.com/controller/action/value1/value2/value3/

The problem comes up, if param3 is left out

www.domain.com/controller/action/value1/value2//value4/

Is there are way to tell Cake to handle the // between param2 und param4 as a unset param3? Currently the paramters are assigned to "wrong" variables.

$param1= value1
$param2= value2
$param3= value4
$param4= NULL

What I want to have is the following

$param1= value1
$param2= value2
$param3= NULL
$param4= value4

Any help here is highly appreciated.

Thanks a lot guys..

Best, Sebastian

2
it should be "public function action()" - and why are you using passed params like this if you cannot guarantee the order? use named/query params instead then.mark

2 Answers

3
votes

You can't do that with CakePHP, and even more in PHP. When you define param's order, every parameters needs to have a value.

I suggest you use named parameters, you'll be able to have:

http://www.example.com/controller/action/param1:value1/param2:value2/param4:value4/

See the documentation for named parameters for more informations.

Edit: I'm not sure, but you should also be able to do this :

www.domain.com/controller/action/value1/value2//value4/

But the option of named parameters is definitely better !

0
votes

you can pass your 3rd and 4th param in such way that the one which is not null must pass in first place then second where you are generating your url.