缺少带有可选参数的闭包参数

Tri*_*cum 6 parameters closures optional laravel laravel-4

我正在通过Laravel 4的几个教程工作,我遇到了一个障碍,我无法弄清楚或理解为什么它运行不正确.

我想要做的是组成一个查看URL的路由,然后根据它进行逻辑工作.这是我目前的代码:

Route::get('/books/{genre?}', function($genre)  
{  
    if ($genre == null) return 'Books index.';  
    return "Books in the {$genre} category.";  
});
Run Code Online (Sandbox Code Playgroud)

因此,如果URL是http://localhost/books,则页面应返回"Books index".如果URL读取http://localhost/books/mystery页面应返回"神秘类别中的书籍".

但是我得到一个'missing argument 1 for {closure}()'错误.我甚至提到了Laravel文档,他们的参数形式完全相同.任何帮助,将不胜感激.

Fer*_*oya 9

如果类型是可选的,则必须定义默认值:

Route::get('/books/{genre?}', function($genre = "Scifi")  
{  
    if ($genre == null) return 'Books index.';  
    return "Books in the {$genre} category.";  
});
Run Code Online (Sandbox Code Playgroud)