gpt4 book ai didi

php - Symfony3 登录后保持语言环境

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:43:35 24 4
gpt4 key购买 nike

我遇到的问题是,当用户在登录页面中更改语言时 - 它有效,但在用户登录后 - 它再次恢复为默认设置。如何使登录前选择相同语言的用户保持登录后保留?我试过在 stackoverflow 上查找这个,但找不到任何工作结果。

安全.yml:

security:

encoders:
AppBundle\Entity\User:
algorithm: bcrypt

role_hierarchy:
ROLE_ADMIN: ROLE_PREMIUM
ROLE_PREMIUM: ROLE_USER

providers:
our_db_provider:
entity:
class: AppBundle:User
property: email

in_memory:
memory: ~

firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false

main:
anonymous: ~

form_login:
#galima nurodyti kur nukreipia loginas
login_path: login
check_path: login
csrf_token_generator: security.csrf.token_manager
logout:
path: /logout

pattern: ^/
http_basic: ~
provider: our_db_provider
access_denied_url: homepage

路由.yml

app:
resource: "@AppBundle/Controller/"
type: annotation
prefix: /{_locale}
requirements:
_locale: lt|en|ru

root:
path: /
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /%locale%/
permanent: true

login:
path: /{_locale}/login
defaults: { _controller: AppBundle:Security:login }
requirements:
_method: GET
_locale: lt|en|ru

logout:
path: /logout
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /{_locale}/login
permanent: true

register:
path: /{_locale}/register
defaults: { _controller: AppBundle:Registration:register }
requirements:
_method: GET
_locale: lt|en|ru

语言更改者:

<ul class="top-menu-list top-menu-languages">
<li><a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'_locale': 'lt'})) }}">LT</a></li>
<li><a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'_locale': 'en'})) }}">EN</a></li>
<li><a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'_locale': 'ru'})) }}">RU</a></li>
</ul>

如有任何想法或示例,我们将不胜感激!

最佳答案

默认情况下,安全组件在名为 _security.main.target_path 的 session 变量中保留最后一个请求 URI(例如 /en/admin)的信息(带有main 是防火墙的名称,在 security.yml 中定义)。成功登录后,用户将被重定向到此路径,以帮助他们从上次访问的已知页面继续。

Note: No matter how many times the language is changed on the login page, because the firewall always redirect to /en/admin/ after success login, so the locale changes again to en.

要解决这个问题,您可能需要 change the default Target Path Behavior :

异常监听器类:

// src/AppBundle/Security/Firewall/ExceptionListener.php

use Symfony\Component\Security\Http\Firewall\ExceptionListener as BaseExceptionListener;

class ExceptionListener extends BaseExceptionListener
{
use TargetPathTrait;

protected function setTargetPath(Request $request)
{
if ($request->hasSession() && $request->isMethodSafe(false) && !$request->isXmlHttpRequest()) {
$this->saveTargetPath(
$request->getSession(),
// the firewall name
'admin',
// save the route name instead of the URI
$request->attributes->get('_route')
);
}
}
}

这会在使用当前语言环境登录后生成旧路由。

配置:

对于 Symfony 2:

# app/config/services.yml
parameters:
# ...
security.exception_listener.class: AppBundle\Security\Firewall\ExceptionListener

对于 Symfony 3:

您可能需要创建编译器传递并手动更改此类:

// src/AppBundle/DependencyInjection/Compiler/ExceptionListenerPass.php;

class ExceptionListenerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$definition = $container->getDefinition('security.exception_listener.admin');
$definition->setClass('AppBundle\Security\Firewall\ExceptionListener');
}
}

最后在您的包中注册编译器传递:

// src/AppBundle/AppBundle.php

class AppBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new ExceptionListenerPass());
}
}

关于php - Symfony3 登录后保持语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41679697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com