Hi,
I have try many ways to create my urls for multilanguage support, my urls looks like this:
https://mywebsite.com/fr/connexion
https://mywebsite.com/en/login
https://mywebsite.com/fr/inscription
https://mywebsite.com/en/register
I want to generate this kind of urls in my router, and also can call the url from urlFor function.
So I have try 3 ways:
Way 1 Router :
$app->group('/{lang}', function (RouteCollectorProxy $group)
{
$group->get('/connexion', App\Controllers\PagesController::class.':login')->setName('login_fr');
$group->get('/login', App\Controllers\PagesController::class.':login')->setName('login_en');
});
Way 1 urlFor:
$routeParser = RouteContext::fromRequest($request)->getRouteParser();
$url = $routeParser->urlFor('login_fr', ['lang'=>'fr']);
Way 2 Router :
$app->get('/fr/connexion', App\Controllers\PagesController::class.':login')->setName('login_fr');
$app->get('/en/login', App\Controllers\PagesController::class.':login')->setName('login_en');
});
Way 2 urlFor:
$routeParser = RouteContext::fromRequest($request)->getRouteParser();
$url = $routeParser->urlFor('login_fr');
Way 3 Router :
$app->get('/{router:fr/connexion|en/login}', App\Controllers\PagesController::class.':login')->setName('login');
Way 3 urlFor (doesn’t work):
$routeParser = RouteContext::fromRequest($request)->getRouteParser();
$url = $routeParser->urlFor('login'); // doesn't work
For now I think the best is “way 2” but I like the “way 3 router” because no repeat “$app->get”, but in the “way3 urlFor” I can’t pass the lang for get the url, this only work if I do this:
$url = $routeParser->urlFor('login', ['router'=>'/fr/connexion']
But it’s stupid because I pass the url for obtain the same url.
So I don’t know if you have a better solution / way / idea, for did what I want (multilangue url router & urlFor call with specific language) ?
Thanks