Hi,
There seems to be an issue with having default values for methods in a controller.
public function get_orders(Request $request, $id){} // This works fine
public function get_orders(Request $request, $id = "test") {} // This does not
The parameter $id
will always be set as its default value inside the function.
Example Route:
$app->get('/orders/{id}', "App\Http\API\Orders\Orders@get_orders");
Example Controller:
class Orders extends CoreApi {
public function get_orders(Request $request, $id = "test"){
...
}
}
Example URL:
www.myapp.com/orders/MY_VALUE
I can see that in Container.php on line 524 lumen is calling the function get_orders using call_user_func_array with the following array even though the function only takes two parameters ($request and $id).
Array (
[0] => Illuminate\Support\Facades\Request Object ( )
[1] => test
[2] => MY_VALUE
)
It appears that the default values are not being overridden with the actual values and instead are being simply appended to the end of the array.
From what I can gather this bad array is being created in the function getMethodDependencies(). It seems to be merging two separate arrays.
protected function getMethodDependencies($callback, $parameters = [])
{
$dependencies = [];
foreach ($this->getCallReflector($callback)->getParameters() as $key => $parameter)
{
$this->addDependencyForCallParameter($parameter, $parameters, $dependencies);
}
return array_merge($dependencies, $parameters);
}
The $dependencies
array is all the parameters from the get_orders
function signature found by using reflection.
The $parameters
array is the values provided by the router which are taken out of the URL.
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:
From that code I don't see how the parameters provided from the URL could ever overwrite the defaulted $id
parameters value?
The 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 hope this comes across clearly.
Is this an issue with the code or is the router not designed to have functions with defaulted values parameters?