I am in the process of working my way through a couple tutorials for Laravel 4 and I have run into a snag that I cannot figure out or comprehend as to why it is running incorrectly.
What I am trying to do compose a route that looks at the URL, and then works logically based on that. 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 "Books index." If the URL reads http://localhost/books/mystery
the page should return "Books in the mystery category."
However I am getting a 'Missing argument 1 for {closure}()' error. I have even referred to the Laravel documentation and they have their parameters formated exactly the same way. Any help would be appreciated.
If the genre is optional, you have to define a default value:
Route::get('/books/{genre?}', function($genre = "Scifi")
{
if ($genre == null) return 'Books index.';
return "Books in the {$genre} category.";
});
Genre is optional, you must define a default value to $genre
. $genre=null
so that it matches for "Book index" of your code.
Route::get('books/{genre?}', function($genre=null)
{
if (is_null($genre))
return "Books index";
return "Books in the {$genre} category";
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With