可选路线参数在Lumen 5.7中不起作用

时间:2019-04-26 06:50:12

标签: php laravel laravel-routing lumen laravel-route

我已经按照以下方法定义了路由和控制器

arr[i]**2

下面是函数定义

$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分配给函数i的第二个参数。 order_id。如果我同时通过两个参数,那么一切都会按预期进行。我只是感到困惑,是我的配置有问题还是accountId本身对可选路由参数有问题?

考虑我触发了Lumen,然后http://localhost/lumen/api/v1/order/get-order-status/ORD1234是分配给ORD1234和'0'分配给accountId

3 个答案:

答案 0 :(得分:1)

可选路线参数如下所示

$router->get('/order/get-order-status/{order_id}/{account_id?}' // see the ? mark

尽管我不确定为什么将0分配给orderId,

通常,控制器方法的第一个参数是请求对象,因此您可以轻松识别请求包含的内容。

public function getOrderStatus(Request $reqeust, $orderId, $accountId = false)

答案 1 :(得分:0)

我认为您应该使用可选参数

  

{account_id?}而不是[/{account_id}]

答案 2 :(得分:0)

移出组外的可选参数路由

$router->group(['prefix' => 'api/v1'], function ($router) {
 $router->get('/order/get-order-status/{order_id}[/{account_id}]'
            , [
            ,'middleware' => 'auth',
            'uses' => 'Api\V1\OrderController@getOrderStatus'
            , 'as' => 'getOrderStatus'
            ]
 );

或者像下面这样的代码

$router->get('api/v1/order/get-order-status/{order_id}[/{account_id}]', 
                    ['uses' => 'Api\V1\OrderController@getOrderStatus',
                    'middleware' => 'auth',
                    'as' => 'getOrderStatus'
                    ]
         );