1

I had defined my route and controller as following

$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'
                ]
     );
  });
});

following is the function defination

public function getOrderStatus($orderId, $accountId = false)
{
   // my code goes here
}

Here the problem is whenever, I skip the optional account_id from the route, then passed order_id is assigned to the 2nd parameter of the function i,e. accountId. If I pass both params then everything is working as expected. I'm just confused whether something is wrong in my configuration or Lumen itself have some issue with optional route params?

Consider I had triggered http://localhost/lumen/api/v1/order/get-order-status/ORD1234 then ORD1234 is assigned to accountId and '0' is assigned to orderId

Mahesh.D
  • 1,691
  • 2
  • 23
  • 49
  • Your url and your route are different. On your route you say it has to be like this: http://localhost/lumen/api/v1/order/get-order-status/ORD1234[/1]. however the route your are triggering is http://localhost/lumen/api/v1/order/get-order-status/ORD1234 – pascal zoet Apr 26 '19 at 08:50

3 Answers3

1

optional route parameters are given like below,

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

though I am not sure why 0 is assigned to the orderId,

and usually, the first parameter to the controller method is a request object, so you can easily identify what are the things that the request contains.

public function getOrderStatus(Request $reqeust, $orderId, $accountId = false)
Shobi
  • 10,374
  • 6
  • 46
  • 82
0

move out your optional parameter route outside the group

$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'
            ]
 );

or like this following code

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

I think you should use optional parameter like

{account_id?} rather then [/{account_id}]

Md. Miraj Khan
  • 377
  • 3
  • 15