在Symfony 4中将路由重定向到语言前缀

时间:2018-10-10 12:26:12

标签: php symfony

从4.1版开始,Symfony现在无需外部插件(https://symfony.com/blog/new-in-symfony-4-1-internationalized-routing)即可处理多语言路由。我已经成功翻译了我的路线,并且一切正常,除了访问根网址(“ /”)时,Symfony呈现404错误,而不是重定向到诸如“ / en /”之类的语言文件夹。

我已经做过一些研究,但是我发现的大多数内容都已经过时了(主要是Symfony 2)。也遇到了这个Symfony 3 Redirect All Routes To Current Locale Version,但对于这样一个简单的问题来说,这似乎是一个非常繁琐的解决方案。

理想情况下,我也想将URL“ / admin”重定向到“ / en / admin”,但是如果我无法实现,我可以接受。

这是我的route / annotations.yaml文件:

def get_train_test(df, y_col, x_cols, ratio):
""" 
This method transforms a dataframe into a train and test set, for this you need to specify:
1. the ratio train : test (usually 0.7)
2. the column with the Y_values
"""
mask = np.random.rand(len(df)) < ratio
df_train = df[mask]
df_test = df[~mask]

Y_train = df_train[y_col].values
Y_test = df_test[y_col].values
X_train = df_train[x_cols].values
X_test = df_test[x_cols].values
return df_train, df_test, X_train, Y_train, X_test, Y_test

y_col_glass = 'Type'
x_cols_glass = list(df_glass.columns.values)
x_cols_glass.remove(y_col_glass)

train_test_ratio = 0.7
df_train, df_test, X_train, Y_train, X_test, Y_test = get_train_test(df_glass, y_col_glass, x_cols_glass, train_test_ratio)

我的翻译.yaml文件:

 File "/Users/blabla/.py", line 120, in <module>
    x_cols_glass.remove(y_col_glass)
   line 1576, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

本地路由配置:

controllers:
    resource: ../../src/Controller/
    type: annotation
    prefix:
        fr: '/fr'
        en: '/en'
        de: '/de'

1 个答案:

答案 0 :(得分:5)

您似乎没有匹配/的路由,可能是因为您所有的路由都以语言代码作为前缀。

您可以使用命令行工具对此进行调试:

php bin/console router:match "/"

如果成功,则应返回以下内容:

 [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                                   |
+--------------+-------------------------------------------------------------------------------------------+

如果未成功,则有多个选项。您可以为“ /”路由创建一个侦听器,以尝试确定正确的语言环境,然后重定向到正确的语言环境,或者始终始终重定向到您的“主要”语言,例如使用这样的路线:

homepage:
    path: /
    controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction
    defaults:
        path: /en
        permanent: true

修改说明

您的路线/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