2017-01-16 102 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> 

任何意見或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將不勝感激!

+0

使用用戶登錄監聽器,並重定向到引用者:http://stackoverflow.com/questions/11180351/symfony2-after-successful-login-event-perform-set-of-動作 – COil

+0

整個示例很久以前不推薦使用... – JustinasT

+0

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

回答

3

缺省情況下,安全組件保留在名爲_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 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') 
      ); 
     } 
    } 
} 

這產生與當前區域設置登錄後的老路子。

配置:

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

您的解決方案將用戶重定向到已設置的頁面。我需要能夠重定向用戶irl。他在登錄前嘗試訪問。例如,我嘗試訪問website.com/en/about - 我重定向到登錄屏幕。在登錄屏幕上,我將語言更改爲ru,登錄後我應該重定向到website.com/ru/about – JustinasT

+0

如果您使用的是Symfony2,則第二部分是您要尋找的解決方案。 – yceruto

+0

我用Symfony3更簡單的解決方案更新了我的答案:) – yceruto