stack
stack

Reputation: 10228

Optional parameter for Laravel routing

I want to make a route for these two URLs:

http://localhost/myweb/public/compare
http://localhost/myweb/public/compare/fb

Here is the rout I've written:

Route::get('compare/{fb?}', 'compareController@index');

It doesn't work as expected. Because it also works for this:

http://localhost/myweb/public/compare/anythingElse

While I want to devote it only for those two URLs. How can I do that?

Upvotes: 2

Views: 71

Answers (1)

stack
stack

Reputation: 10228

All I need to do is using where() method. Something like this:

Route::get('compare/{fb?}', 'compareController@index')->where('fb', 'fb');

->where('fb', 'fb') restricts that optional parameter to only the word of fb literally.

Reference

Upvotes: 1

Related Questions