Missing argument to close with optional parameter

I am working on several tutorials for Laravel 4, and I ran into a problem that I cannot understand or understand why it does not work correctly.

What I'm trying to do is make a route that looks at the url and then works on it logically. Here is my current code:

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

So, if the URL is http://localhost/books , the page should return a Books index. If the URL reads http://localhost/books/mystery , the page should return "Books in the category of mystery."

However, I get "Missing argument 1 for error {close} ()". I even referred to the Laravel documentation, and their parameters were configured exactly the same. Any help would be appreciated.

+6
source share
2 answers

If the genre is optional, you must specify a default value:

 Route::get('/books/{genre?}', function($genre = "Scifi") { if ($genre == null) return 'Books index.'; return "Books in the {$genre} category."; }); 
+9
source

Genre is optional, you must specify the default value of $genre . $genre=null to match the Book Index parameter of your code.

 Route::get('books/{genre?}', function($genre=null) { if (is_null($genre)) return "Books index"; return "Books in the {$genre} category"; }); 
+1
source

Source: https://habr.com/ru/post/946213/


All Articles