Ok, what does the error say?
Default PHP function parameters break routes
Hi,
I seem to be having an issue with having default values in my controller methods.
The route:
$app->get('/orders/{id}', "App\Http\API\Orders\Orders@get_orders");
The controller:
class Orders extends CoreApi {
public function get_orders(Request $request, $id = "test"){
...
}
}
However inside the function the parameter $id will always have the default value.
After looking through the codebase it seems to be an issue with the array of parameters are are being passed to the function when it is called?
In a case where the URL is: /orders/MY_VALUE the function appears to be called with the following array:
Array (
[0] => Illuminate\Support\Facades\Request Object ( )
[1] => test
[2] => MY_VALUE
)
From what I can gather it seems like there are two array which are merged.
The first array is all the parameters from my php function get_orders found by using reflection (Request $request, $id = "test") and a second array with any values that should overwrite the default values where needed.
From what i can tell this is the code in lumen where the array is created which is later passed my function. Line 549 of the Container.php File:
protected function getMethodDependencies($callback, $parameters = [])
{
$dependencies = [];
foreach ($this->getCallReflector($callback)->getParameters() as $key => $parameter)
{
$this->addDependencyForCallParameter($parameter, $parameters, $dependencies);
}
return array_merge($dependencies, $parameters);
}
I can see that before the array merge happens $dependencies has a value of:
array(
[0] => Illuminate\Support\Facades\Request Object ( )
[1] => test
)
and $parameters has a value of:
array(
[0] => MY_VALUE
)
From that code I don't see how the parameters provided from the URL could ever overwrite the defaulted id parameters value? Array merge just seems to append the real value of id to the end of the array. This means the function which takes two parameters (Request $request, $id = "test") is passed three parameters with the real value not being assigned to any variable in the signature of the method.
I should note that if i take out the $id default parameter value of "test" it works all okay...
For example:
public function get_orders(Request $request, $id){} // This works fine
public function get_orders(Request $request, $id = "test") {} // This does not, the value of $id is "test" no matter what
I am confused as to if I have an issue or there is a bug in the lumen codebase?
Please or to participate in this conversation.