ATIKON ATIKON - 8 months ago 45
PHP Question

Route with parameter laravel 5.2

I was descriped routes.php as

Route::get('/{id}', 'HomeController@index');

And I want to use HomeController without parameter too. How to do it?

if url without parameter, I also want to use HomeController to display homepage, if url with parameter I want to display some data using controller.

Answer Source

You want to use optional parameters:

Route::get('/{id?}', 'HomeController@index');

Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name.