php - Multi-language URLs in Laravel
For a "welcome" page in my Laravel application, I need to have a route for 4 different languages. Currently I have made it like this:
# German
Route::get('/willkommen', 'WaitingListController@index')->name('waiting_list.index');
# French
Route::get('/bienvenu', 'WaitingListController@index')->name('waiting_list.index');
# Italian
Route::get('/benvenuto', 'WaitingListController@index')->name('waiting_list.index');
# Spanish
Route::get('/bienvenidos', 'WaitingListController@index')->name('waiting_list.index');
This is fine for one page I suppose, but I will need to translate URLs for every page on the site eventually. This becomes cumbersome, as I would have to make any future change 4 times. That's clearly something to avoid.
So my question is: what is the best way to do this? I would prefer not to have to create 4 routes for each page in routes/web.php
. Is there a more elegant solution? Can I somehow pass an array to the route instead of a string perhaps?
Answer
Solution:
Expanding on my comment, something like this could work
Route::get('/{welcome}', 'WaitingListController@index')
->where('welcome', 'willkommen|bienvenu|bienvenidos')
->name('waiting_list.index');
Source