Parameters 缺少带有可选参数的闭包的参数
我正在学习Laravel 4的一些教程,我遇到了一个障碍,我无法理解它为什么运行不正确 我所要做的是构建一个查看URL的路由,然后基于该路由进行逻辑工作。这是我目前的代码:Parameters 缺少带有可选参数的闭包的参数,parameters,closures,laravel,laravel-4,optional,Parameters,Closures,Laravel,Laravel 4,Optional,我正在学习Laravel 4的一些教程,我遇到了一个障碍,我无法理解它为什么运行不正确 我所要做的是构建一个查看URL的路由,然后基于该路由进行逻辑工作。这是我目前的代码: Route::get('/books/{genre?}', function($genre) { if ($genre == null) return 'Books index.'; return "Books in the {$genre} category."; }); 因此,如果URL
Route::get('/books/{genre?}', function($genre)
{
if ($genre == null) return 'Books index.';
return "Books in the {$genre} category.";
});
因此,如果URL是http://localhost/books
,如果URL显示为http://localhost/books/mystery
页面应返回“神秘类别中的书籍”
但是,我得到了一个“缺少{closure}()的参数1”错误。我甚至参考了Laravel文档,它们的参数的格式完全相同。任何帮助都将不胜感激。如果流派是可选的,您必须定义一个默认值:
Route::get('/books/{genre?}', function($genre = "Scifi")
{
if ($genre == null) return 'Books index.';
return "Books in the {$genre} category.";
});
流派是可选的,您必须将默认值定义为
$Genre
$genre=null
,以便与代码的“图书索引”匹配
Route::get('books/{genre?}', function($genre=null)
{
if (is_null($genre))
return "Books index";
return "Books in the {$genre} category";
});
谢谢你这么快的回复。工作起来就像一个符咒。您还可以为路由提供
默认值
,因此路由::get('books/{genre?}',函数($genre){return$genre;})->默认值('genre',谜')代码>错误原因如果流派为空,流派将在最后一个函数中具有“Scfi”值。你可以意识到“图书索引”永远不会显示