Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mironmg's avatar

Multilanguage Routing Prefix

Hi guys...so I have a problem that ate my last 2 hours and I still haven't figured it out. I'm starting to believe that it's impossible to achieve but still I decided to ask in here So I have a multilanguage website... for blog posts and others I am using Model<->ModelTranslation relationship to gather dynamic slugs and all. Now I also need dynamic slugs for the hardcoded pages so to speak... The routing for these looks like this

Route::group(
    [
        'prefix' => '{locale}', 
        'where' => ['locale' => '[a-zA-Z]{2}'],
        'middleware' => 'localization'
    ], function(){
    ...
        Route::get('/pricing', 'FrontPageController@products')->name('products');
    ...
     }
);

This results in The following routes:

site.com/en/pricing
site.com/fr/pricing
site.com/cn/pricing

But obviously this is not an ideal setup What I want to achieve is to gather the "locale" parameter passed in the prefix to assign a dynamic slug to that pricing string in order to have more user-friendly URLs

Creating databases is not a solution because the website is already in production and this is more of a "nice to have" feature rather than something crucial.

So if I manage to access that {locale} variable in the second part of the function it would be great because I could create a config file with different variations... and have something like

Route::get(config('routing')['services'][$lang], 'FrontPageController@products')->name('products');

Thanks a lot

Marian

0 likes
1 reply
Sinnbeck's avatar

Perhaps something like this?

Your route

Route::get('/{locale}-test', function($locale) {
    return $locale;
})->middleware('localization');

Your middlware

public function handle($request, Closure $next)
    {
        $locale = $request->route()->parameter('locale');
        $request->route()->setParameter('locale', config('routing')['services'][$locale]);

        return $next($request);
    }

Please or to participate in this conversation.