如何将第二个参数作为可选项传递给LUMEN中的路线?
问题描述:
My routes/web.php file
$router->group(['prefix' => 'api/v1'], function () use ($router) {
$router->get('post/{string}/comment/{length?}', 'PostController@index');
});
My controller file
public function index($string, $length = 0){
// boy
}
URLs to execute
localhost/project/public/api/v1/post/abcd/comment/1
OR
localhost/project/public/api/v1/post/abcd/comment
I want string and length values in my controller, string is not optional parameter but length is optional if i not provide it it should take 0
答
Lumen uses a different router so you need to define optional parameters a little differently:
From the documentation
$app->get('user[/{name}]', function ($name = null) {
return $name;
});
So in your case it would be:
$router->get('post/{string}/comment[/{length}]', 'PostController@index');