Search code examples
laravelroutesmiddleware

Laravel - Add Route Wildcard to every Route


I have created a multilanguage application in laravel and for every route (because i want to see in the url what my language is) i need

www.example.com/{locale}/home 

for example, whereas {locale} is the set language and home well, is home. but for every route i need to declare that locale wildcard. is there any way to get this done with middleware or something, to add this before route is executed? Thanks!


Solution

  • You can use prefix for it.

    Route::group(['prefix' => '{locale}'], function () {
        Route::get('home','Controller@method');
        Route::get('otherurl','Controller@method');
    });
    

    And here how you can access it now.

    www.example.com/{locale}/home 
    www.example.com/{locale}/otherurl
    

    For more info. https://laravel.com/docs/5.8/routing#route-group-prefixes