2017-01-16 2 views
5

문제 로그인 페이지에서 사용자가 언어를 변경하면 작동하지만 사용자 로그인 후에 다시 기본으로 돌아갑니다. 로그인하기 전에 선택한 동일한 언어 사용자가 로그인 한 후에도 계속 사용할 수있게하려면 어떻게해야합니까? stackoverflow에서이 문제를 조사해 보았지만 결과를 찾을 수 없습니다.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> 

어떤 아이디어 또는 전자 :

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는 평가 될 것입니다! 기본적

+0

는 사용자 로그인 수신기를 사용하고 리퍼러로 리디렉션 : http://stackoverflow.com/questions/11180351/symfony2-after-successful-login-event-perform-set-of- actions – COil

+0

전체 예제는 오래 전부터 사용되지 않습니다 ... – JustinasT

+0

http://symfony.com/doc/current/components/security/authentication.html#authentication-events – COil

답변

3

는 보안 컴포넌트 (mainsecurity.yml 정의 방화벽의 이름 인)와 _security.main.target_path라는 세션 변수의 마지막 요청 URI (예컨대 /en/admin)의 정보를 유지한다. 로그인에 성공하면 사용자는이 경로로 리디렉션되어 방문한 마지막으로 알려진 페이지부터 계속할 수 있습니다.

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') 
      ); 
     } 
    } 
} 

이 현재의 로케일로 로그인 후 이전 경로를 생성합니다.

구성 : 심포니 2의

:

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

:

당신은 컴파일러 패스를 만들 필요가 수동으로이 클래스를 변경할 수 있습니다

// 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()); 
    } 
} 
+0

솔루션은 사용자를 설정된 페이지로 리디렉션합니다. 사용자 irl을 리디렉션 할 수 있어야합니다. 그는 로그인하기 전에 액세스를 시도했습니다. 예를 들어 나는 website.com/en/about에 접속하려고 시도합니다 - 로그인 화면으로 리디렉션됩니다. 로그인 화면에서 언어를 ru로 변경하고 로그인 후에 website.com/ru/about – JustinasT

+0

으로 리디렉션되어야합니다. Symfony2를 사용하는 경우 두 번째 부분은 찾고자하는 해결책입니다. – yceruto

+0

symfony3에 대한 간단한 해결책으로 답변을 업데이트했습니다. – yceruto