Reputation: 638
I wanna write a route Route::get('/{lang}/home', 'ExampleController@get_home')
, so
so lang maybe exists or not?
How can I do this?
Upvotes: 0
Views: 486
Reputation: 8287
Laravel doesn't allow optional parameter in middle of route. However, you can resolve it by adding 2 route like this
Route::get('/home', 'ExampleController@get_home')
Route::get('/{lang}/home', 'ExampleController@get_home')
Controller (add $lang optional param in your controller action)
class ExampleController extends Controller {
public function get_home(Request $request, $lang = null){
...
}
}
Upvotes: 2
Reputation: 1537
Update your route to:
Route::get('/{lang?}/home', 'ExampleController@get_home')
Upvotes: 0