我已将我的路线和控制器定义如下
$router->group(['prefix' => 'api/v1'], function ($router) {
$router->group(
['middleware' => 'auth'], function() use ($router) {
$router->get('/order/get-order-status/{order_id}[/{account_id}]'
, [
'uses' => 'Api\V1\OrderController@getOrderStatus'
, 'as' => 'getOrderStatus'
]
);
});
});
以下是函数定义
public function getOrderStatus($orderId, $accountId = false)
{
// my code goes here
}
这里的问题是每当我从路线跳过可选的account_id
,然后通过order_id
分配给函数i的第二个参数,e。 accountId
。如果我通过两个参数,那么一切都按预期工作。我只是困惑我的配置是否有问题或者Lumen
本身在可选路由参数方面存在一些问题?
考虑我触发了http://localhost/lumen/api/v1/order/get-order-status/ORD1234
然后ORD1234
被分配给accountId
而'0'被分配给orderId
可选的路由参数如下所示,
$router->get('/order/get-order-status/{order_id}/{account_id?}' // see the ? mark
虽然我不确定为什么0被分配给orderId,
通常,控制器方法的第一个参数是请求对象,因此您可以轻松识别请求包含的内容。
public function getOrderStatus(Request $reqeust, $orderId, $accountId = false)
我认为你应该使用可选参数
{account_id?}
而不是[/{account_id}]