我如何将第二个参数作为可选参数传递给 LUMEN 中的路由?
how can i pass second parameter as optional to the route in LUMEN?
我的 routes/web.php 文件
$router->group(['prefix' => 'api/v1'], function () use ($router) {
$router->get('post/{string}/comment/{length?}', 'PostController@index');
});
我的控制器文件
public function index($string, $length = 0){
// boy
}
要执行的 URL
localhost/project/public/api/v1/post/abcd/comment/1
OR
localhost/project/public/api/v1/post/abcd/comment
我想要我的控制器中的字符串和长度值,字符串不是可选参数但长度是可选的,如果我不提供它应该取 0
Lumen 使用不同的路由器,因此您需要稍微不同地定义可选参数:
$app->get('user[/{name}]', function ($name = null) {
return $name;
});
所以在你的情况下会是:
$router->get('post/{string}/comment[/{length}]', 'PostController@index');
我的 routes/web.php 文件
$router->group(['prefix' => 'api/v1'], function () use ($router) {
$router->get('post/{string}/comment/{length?}', 'PostController@index');
});
我的控制器文件
public function index($string, $length = 0){
// boy
}
要执行的 URL
localhost/project/public/api/v1/post/abcd/comment/1
OR
localhost/project/public/api/v1/post/abcd/comment
我想要我的控制器中的字符串和长度值,字符串不是可选参数但长度是可选的,如果我不提供它应该取 0
Lumen 使用不同的路由器,因此您需要稍微不同地定义可选参数:
$app->get('user[/{name}]', function ($name = null) {
return $name;
});
所以在你的情况下会是:
$router->get('post/{string}/comment[/{length}]', 'PostController@index');