Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将第二个参数作为可选参数传递到LUMEN中的路由?_Php_Laravel_Lumen - Fatal编程技术网

Php 如何将第二个参数作为可选参数传递到LUMEN中的路由?

Php 如何将第二个参数作为可选参数传递到LUMEN中的路由?,php,laravel,lumen,Php,Laravel,Lumen,My 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 } 要

My 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使用不同的路由器,因此您需要稍微不同地定义可选参数:

因此,在您的情况下,它将是:

$router->get('post/{string}/comment[/{length}]', 'PostController@index');

这个密码是怎么回事?看起来不错,这不是拉威尔。。。如果我要的是拉威尔,看起来还可以,但我要的是流明。传递可选参数略有不同。为什么要使用APIfy:之类的包来创建APIfy?控制器方法:index()的函数头是什么样的?$name(可能还有$length)是否会嵌入Lumen请求对象中,或者$name(可能还有$length)是否会作为第二个参数传递给index()?Lumen doc中没有此信息。它们可以在方法签名中作为参数使用,
公共函数索引($string,$length)
$router->get('post/{string}/comment[/{length}]', 'PostController@index');