如何将_locale参数添加到安全路径? [英] How can I add the _locale parameter to security paths?

查看:105
本文介绍了如何将_locale参数添加到安全路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了我的安全设置,以保护根路径 / 下的所有内容,例如,公开页面可以查看隐私策略, /隐私。一切正常。

I setup my security settings to protect everything which is under the root path /, exept for a public page to view the privacy policy, /privacy. Everything works fine.

# security.yml
access_control:
    - { path: ^/privacy$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/,         role: ROLE_USER }

Now我在隐私政策页面上添加了一些不同的翻译,以使路由从 / privacy 更改为 / {_ locale} / privacy 。不幸的是,我无法将 _locale 参数添加到安全路径中,如下所示:

Now I added some different translations to the privacy policy page, so that the route changes from /privacy to /{_locale}/privacy. Unfortunately I cannot add the _locale parameter to the security path like this:

access_control:
    ...
    - { path: ^/{_locale}/privacy$, role: IS_AUTHENTICATED_ANONYMOUSLY }

所以我必须修改 security.yml 以便添加 _locale 到路径并将其限制为预定义的语言,例如(en | fr | es | de)

So how do I have to modify my security.yml so that I can add the _locale to the path and to restrict it to predefined languages, e.g. (en|fr|es|de)?

推荐答案

现在( (从Symfony 4.1或更高版本开始),您可以在一个位置定义语言环境,并在应用程序中的任何地方使用

Nowadays (since Symfony 4.1 or later) you can define the locales in one place and use it everywhere in your application

config / services.yaml 添加

parameters:
    myAppName.locales: en|fr|es|de

config / routes.yaml

cms:
    prefix:   /{_locale}/
    controller: App\Controller\DefaultController::index
    requirements:
        _locale: '%myAppName.locales%'

in config / packages / security.yaml

security:
    ## .... no changes here
    access_control:
        - { path: ^/(%myAppName.locales%)/cms, roles: ROLE_ADMIN }

这篇关于如何将_locale参数添加到安全路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆