Performance: Optional parameters
Optional parameters affect routing and controller method resolution speed during request handling.
Route::get('/user/{id?}', function ($id = null) { return $id; }); Route::get('/user/name/{name}', function ($name) { return $name; });
Route::get('/user/{id?}/{name?}', function ($id = null, $name = null) { return $id . ' ' . $name; });
| Pattern | Route Matching Complexity | Request Delay | Server CPU Load | Verdict |
|---|---|---|---|---|
| Multiple optional parameters in one route | High | 10-20ms delay | Increased | [X] Bad |
| Single optional parameter per route | Low | 5-10ms delay | Normal | [OK] Good |
public function show($id, $page = 1) {
return "ID: $id, Page: $page";
}show(5)?Route::get('/post/{id?}/{comment?}', function ($id = 1, $comment = 'none') {
return "$id - $comment";
});category and an optional page number with default 1. Which of these route and controller method pairs is correct?category must come first without a question mark. The optional page comes after with a question mark.Pass the test and this pattern moves unknown