我正在使用Silex 2,如果使用無效語言環境加載了任何url,我想用默認語言環境重定向到主頁。更改語言環境的Silex重定向
// homepage/root
$this->get('{_locale}/', function (Request $request) use ($app) {
return $app['twig']->render('index/index.html.twig', array());
})->bind('homepage');
中間件之前:
// i18n Control
$locale = $request->getLocale();
$allowLocale = ['en','es','de'];
if (!in_array($locale, $allowLocale)) {
$request->setLocale('en');
$response = new \Symfony\Component\HttpFoundation\RedirectResponse($app['url_generator']->generate('homepage'), 301);
$response->prepare($request);
return $response->send();
}
但這代碼產生無限循環。
我想:
如果用戶插入此網址:/ ES /富那麼一切都OK。
如果用戶插入這個URL:/ fr/foo,那麼他必須重定向到/ en。
感謝。
你能做到這一點更容易,下面檢查我的答案... – mTorres
謝謝你的貢獻,它對我有用。 – cybtow