[英]Redirect route to language prefix in Symfony 4
Since version 4.1, Symfony now handles multilingual routing without the need of an external plugin ( https://symfony.com/blog/new-in-symfony-4-1-internationalized-routing ). 从版本4.1开始,Symfony现在可以处理多语言路由,而无需外部插件( https://symfony.com/blog/new-in-symfony-4-1-internationalized-routing )。 I've translated my routes with success and all works fine except that when accessing the root url ("/"), Symfony renders a 404 error instead of redirecting to a language folder such as "/en/".
我已经成功翻译了我的路线并且一切正常,除了当访问根URL(“/”)时,Symfony呈现404错误而不是重定向到语言文件夹,例如“/ en /”。
I've done some research but most of what i've found is very outdated (mostly Symfony 2). 我做了一些研究,但我发现的大部分内容已经过时了(主要是Symfony 2)。 Also ran into this Symfony 3 Redirect All Routes To Current Locale Version but it seems like an incredibly tedious solution to such a simple problem.
此外,还遇到了Symfony 3将所有路由重定向到当前语言环境版本,但对于这样一个简单的问题,这似乎是一个非常繁琐的解决方案。
Ideally, i'd also like to redirect the url "/admin" to "/en/admin", but if i can't achieve it i can live with that. 理想情况下,我还想将网址“/ admin”重定向到“/ en / admin”,但如果我无法实现它,我可以忍受它。
Here's my routes/annotations.yaml file: 这是我的routes / annotations.yaml文件:
controllers:
resource: ../../src/Controller/
type: annotation
prefix:
fr: '/fr'
en: '/en'
de: '/de'
My translations .yaml file: 我的翻译.yaml文件:
framework:
#default_locale: '%locale%'
default_locale: 'en'
translator:
paths:
- '%kernel.project_dir%/translations'
fallbacks:
- 'en'
Home route configuration: 家庭路线配置:
@Route("/", name="home")
It looks like you don't have a route matching /
, probably because all your routes are prefixed with a language code. 看起来您没有路由匹配
/
,可能是因为您的所有路由都以语言代码为前缀。
You can debug this using the command line tool: 您可以使用命令行工具进行调试:
php bin/console router:match "/"
If it's successful it should return something like this: 如果它成功了它应该返回这样的东西:
[OK] Route "homepage" matches
+--------------+-------------------------------------------------------------------------------------------+
| Property | Value |
+--------------+-------------------------------------------------------------------------------------------+
| Route Name | homepage |
| Path | / |
| Path Regex | #^/$#sD |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | GET |
| Requirements | NO CUSTOM |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: App\Controller\HomeController::index |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+-------------------------------------------------------------------------------------------+
If it is not successful there are multiple options. 如果不成功,有多种选择。 You could create a listener for the "/" route that tries to determine the proper locale and then redirects to the correct one or just always redirect to your "primary" language, eg using a route like this:
您可以为尝试确定正确语言环境的“/”路由创建一个侦听器,然后重定向到正确的语言环境,或者只是始终重定向到“主要”语言,例如使用如下路由:
homepage:
path: /
controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction
defaults:
path: /en
permanent: true
edit for clarification: 编辑澄清:
Your routes/annotation.yaml could then look like this: 您的routes / annotation.yaml可能如下所示:
controllers:
resource: ../../src/Controller/
type: annotation
prefix:
fr: '/fr'
en: '/en'
de: '/de'
home_fallback:
path: /
controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction
defaults:
path: /en
permanent: true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.