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 route I wrote:
Route::get('compare/{fb?}', 'compareController@index');
It doesn't work as expected. Because it also works for this:
http://localhost/myweb/public/compare/anythingElse
Although I want to dedicate it to just these two urls. How can i do this?
+3
stack
source
to share
1 answer
All I have to do is use the method where()
. Something like that:
Route::get('compare/{fb?}', 'compareController@index')->where('fb', 'fb');
->where('fb', 'fb')
restricts this optional parameter to fb
literal only .
Link
+1
stack
source
to share