I was descriped routes.php as
Route::get('/{id}', 'HomeController@index');
And I want to use HomeController without parameter too. How to do it?
Update: 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.
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.
Your id will be passed as a param to the controller function
public function index($id) {
echo $id;
}
if you want to use it without params you will need a optional parameter
Route::get('/{id?}', 'HomeController@index');
change your controller function to
public function index($id = 0) {
if ($id > 0 ) {
echo $id;
} else {
echo "display all";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.