Symfony3登录后保留区域设置
问题描述:
问题我有,当用户更改登录页面中的语言时 - 它工作,但用户登录后 - 它又恢复为默认值。如何使登录前保持选定的语言用户可以保持登录状态? 我试过在*上查找这个,但无法找到任何工作的结果。Symfony3登录后保留区域设置
security.yml:
<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>
任何意见或E:
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
的routing.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
语言的改变xamples将不胜感激!
答
缺省情况下,安全组件保留在名为_security.main.target_path
会话变量的最后一个请求的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 toen
.
为了解决这个问题,你可能需要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')
);
}
}
}
这产生与当前区域设置登录后的老路子。
配置:
为symfony1.2 2:
# app/config/services.yml
parameters:
# ...
security.exception_listener.class: AppBundle\Security\Firewall\ExceptionListener
为symfony1.2 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());
}
}
使用用户登录监听器,并重定向到引用者:http://*.com/questions/11180351/symfony2-after-successful-login-event-perform-set-of-动作 – COil
整个示例很久以前不推荐使用... – JustinasT
http://symfony.com/doc/current/components/security/authentication.html#authentication-events – COil