douhe6255 2018-04-01 08:25
浏览 122
已采纳

登录后的Symfony3语言环境(重定向到默认语言环境)

I have problem in Symfony project with Symfony version 3.1.10. I got this project to finish translating and this is my first meeting with Symfony.

What is problem here: I have two language DE and EN, default language is DE. When someone go to login page or home page( where also is login form) and switch to EN and enter to login, its change locale to /de/ default language, after login. I spent few days on this bug and really I need help. I tried everything, but there is still no progress.

I followed Symfony documentation, how to configure translation, how to use Sticky user session and I created everything same, but bug is there agin. I will show my code below:

config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }

parameters:
    locale: de
framework:
    #esi:             ~
    translator:      { fallbacks: ["%locale%"] }
    default_locale:  "%locale%"

app/config/routing.yml

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

proreg:
    path:     /{_locale}/product/registration
    defaults:  { _controller: ProductRegistration\Controller\ProductRegistrationController::productRegistrationAction }
prolist:
    path:     /{_locale}/profile/products
    defaults:  { _controller: ProductRegistration\Controller\ProductRegistrationController::listAction }
prodel:
    path:     /{_locale}/profile/products/delete/{id}
    defaults:  { _controller: ProductRegistration\Controller\ProductRegistrationController::deleteAction }
proadd:
    path:     /{_locale}/product/register
    defaults:  { _controller: ProductRegistration\Controller\ProductRegistrationController::createAction }

app/config/security.yml

security:
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

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

        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                login_path:   fos_user_security_login
                # csrf_token_generator: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4
                always_use_default_target_path: false
            logout:       true
            anonymous:    true
            # http_basic: ~
            # http://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate

            # form_login: ~
            # http://symfony.com/doc/current/cookbook/security/form_login_setup.html

    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }
        - { path: ^/sso/login_check, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/sso/settoken, role: ROLE_ADMIN }
        - { path: ^/profile/products, role: ROLE_ADMIN }
        - { path: ^/product/registration, role: ROLE_ADMIN }

app/Resources/FOSUserBundle/all.xml

<?xml version="1.0" encoding="UTF-8" ?>

<routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

    <import
        resource="@FOSUserBundle/Resources/config/routing/security.xml"/>
    <import
        resource="@FOSUserBundle/Resources/config/routing/profile.xml"
        prefix="/{_locale}/profile" />
    <import
        resource="@FOSUserBundle/Resources/config/routing/registration.xml"
        prefix="/{_locale}/register" />
    <import
        resource="@FOSUserBundle/Resources/config/routing/resetting.xml"
        prefix="/{_locale}/resetting" />
    <import
        resource="@FOSUserBundle/Resources/config/routing/change_password.xml"
        prefix="/{_locale}/profile" />
</routes>

app/Resources/FOSUserBundle/security.xml

<?xml version="1.0" encoding="UTF-8" ?>

<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

    <route id="fos_user_security_login" path="/{_locale}/login" methods="GET POST">
        <default key="_controller">FOSUserBundle:Security:login</default>
    </route>

    <route id="fos_user_security_check" path="/login_check" methods="POST">
        <default key="_controller">AppBundle:Security:check</default>
    </route>

    <route id="fos_user_security_logout" path="/logout" methods="GET POST">
        <default key="_controller">FOSUserBundle:Security:logout</default>
    </route>

</routes>

src/AppBundle/Controller/DefaultController.php

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Security;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;

class DefaultController extends Controller
{
    /**
     * @Route("/{_locale}", name="homepage", defaults={"_locale": "de"})
     */
    public function indexAction(Request $request)
    {
        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
        ]);
    }

src/AppBundle/Controller/SecurityController.php

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

// src/UserBundle/Controller/SecurityController.php
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Controller\SecurityController as BaseController;

class SecurityController extends BaseController
{
    public function loginAction(Request $request)
    {
        $response = parent::loginAction(request);

        // ... do custom stuff

      $errors->messageData = "Test";
      $errors->messageKey = "test";

        $error = $error ? $error : $errors;
        return $this->renderLogin(array(
            'last_username' => $lastUsername,
            'error' => $error,
            //'csrf_token' => $csrfToken,
            'origin' => $request->get("origin")));
    }
    public function checkAction()
    {
        $response = parent::checkAction();
        die  ("<pre>" .print_r($response). "</pre>");
    }
}

src/AppBundle/Event/LocaleListener.php

// src/AppBundle/LocaleListener.php
namespace AppBundle\Event;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LocaleListener implements EventSubscriberInterface
{
    private $defaultLocale;

    public function __construct($defaultLocale = 'de')
    {
        $this->defaultLocale = $defaultLocale;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
       $request = $event->getRequest();
       if (!$request->hasPreviousSession()) {
           return;
        }
        //print_r($request->get('_locale'));
        // try to see if the locale has been set as a _locale routing parameter
        if ($locale = $request->attributes->get('_locale')) {
            $request->getSession()->set('_locale', $locale);
        } else {
            // if no explicit locale has been set on this request, use one from the session
            $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
        }
    }

    public static function getSubscribedEvents()
    {
        return array(
            // must be registered after the default Locale listener
            KernelEvents::REQUEST => array(array('onKernelRequest', 15)),
        );
    }
}

login form

<form action="{{ path("fos_user_security_check") }}" method="post" id="login-form" class="col-xs-12">
<div class="content">
   <h2>{{ 'index.login.regcust'|trans }}</h2>
   <p>{{ 'index.login.haveaccount'|trans }}</p>
   <ul class="form-list">
      <li class="form_element">
         <label for="email" class="required">{{ 'index.login.email'|trans }}<em>*</em></label>
         <div class="input-box"> 
            <input type="hidden" name="origin" value="{{ origin }}" />
            <input type="hidden" name="_target_path" value="{{ path("setSSOToken") }}" />
            <input type="email" name="_username" value="" id="username" class="input-text required-entry validate-email" title="{{ 'index.login.email'|trans }}">
         </div>
      </li>
      <li class="form_element">
         <label for="pass" class="required">{{ 'index.login.password'|trans }}<em>*</em></label>
         <div class="input-box input-box-password">
            <input type="password" name="_password" class="input-text required-entry" id="password" title="{{ 'index.login.password'|trans }}">
            <!--a href="" class="f-left forgot-password">{{ 'index.login.forgot'|trans }}</a-->
         </div>
      </li>
   </ul>
   <p class="required required_hint">
      <em>*</em> {{ 'index.login.required'|trans }}
   </p>
</div>
<div class="buttons-set">
   <div class="button_wrapper">
      <button type="submit" class="button" title="Anmelden" name="send" id="send2"><span><span>{{ 'index.login.submit'|trans }}</span></span></button>
   </div>
</div>
</form>

If someone can help me how to fix this problem, i will be very grateful. Thanks

  • 写回答

1条回答 默认 最新

  • douyan2470 2018-04-03 13:34
    关注

    OK I am sure this is related to your config. Let's try one more time programmatically:

    1) In src/AppBundle/Event/LocaleListener.php change

    namespace AppBundle\Event;
    

    to

    namespace AppBundle\EventListener;
    

    and make sure it is registered according to https://symfony.com/doc/3.1/session/locale_sticky_session.html:

    services:
    app.locale_listener:
        class: AppBundle\EventListener\LocaleListener
        arguments: ['%kernel.default_locale%']
        tags:
            - { name: kernel.event_subscriber }
    

    2) Remove

    /{_locale}
    

    from app/Resources/FOSUserBundle/security.xml and append

    prefix="/{_locale}"
    

    to

    <import
        resource="@FOSUserBundle/Resources/config/routing/security.xml"
    

    in app/Resources/FOSUserBundle/all.xml. This is mostly just to keep the config in one place.

    3) Make sure you have

    firewalls:
        main:
            form_login:
                login_path: fos_user_security_login
                check_path: fos_user_security_check
                default_target_path: <your_desired_route>
            logout:
                path: fos_user_security_logout
    

    in app/config/security.yml with the locale.

    4) In app/config/security.yml you should define the correct paths for access_control:

    access_control:
        - { path: ^/(en|de)/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/(en|de)/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/(en|de)/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/(en|de)/admin/, role: ROLE_ADMIN }
        - { path: ^/(en|de)/sso/login_check, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/(en|de)/sso/settoken, role: ROLE_ADMIN }
        - { path: ^/(en|de)/profile/products, role: ROLE_ADMIN }
        - { path: ^/(en|de)/product/registration, role: ROLE_ADMIN }
    

    including the locales. Otherwise those paths won't be protected by the firewall. Also you should make sure register, resetting and any other routes correspond to the ones in FOSUserBundle.

    Edit

    It looks like this was related to another controller which altered the FOSUserBundle behavior.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建