Search code examples
phplaravellumenlaravel-10lumen-routing

Lumen: optional route params isn't resolved correctly


Using Laravel's Lumen 9.1.6

I am following the documentation but still have a problem with an optional trailing route parameter defined like this:

routes/web.php

$router->group(['prefix' => 'question'], function() use($router) {  
    $router->get('log/{eid}/{uid}[/{year}]', ['middleware' => 'api.auth', 'uses' => 'QuestionController@getLogs']);
});

and then in QuestionController.php

public function getLogs($eid, $uid, $year = null, Request $request) {
  ....
}

If I then call
api.tld/question/log/1/2/2021 - it works fine, however
api.tld/question/log/1/2 - throws Unable to resolve dependency [Parameter #2 [ $year ]] in class App\Http\Controllers\QuestionController

Lumen docs are very sparse on this (although I believe I've followed the syntax correctly). Any ideas?


Solution

  • Try this way:

    public function getLogs(Request $request, $eid, $uid, $year = null) {
      ....
    }
    

    You have to put request first.