douzhuo6931 2019-04-22 12:01
浏览 38

请求区域设置不同:Controller参数与RequestStack

I'm trying to figure out why my locale is different in one of my controllers and when I get the request from the request stack.

Here's an example of one of my routes: 'website.com/nl'

/**
 * Main page of the website
 * @Route("/{_locale}",
 *     name="home",
 *     defaults={"_locale": "nl"},
 *     requirements={
 *         "_locale": "nl|en|fr"
 *     },
 * )
 * @param Request $request
 * @param $_locale
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function home(
    Request $request,
    $_locale
) {
    // $_locale === "nl"
    // $request->getLocale() === "nl"
}

As expected based on the url, the locale is NL

Now when I need the locale in one of my services or event listeners, I need to use RequestStack->getCurrentRequest()->getLocale(). However, this locale is always set to locale: "en"

Service mapping:

app.doctrine.locale_listener:
    class: App\EventListener\LocaleListener
    public: false
    arguments: ["@request_stack"]
    lazy: true
    tags:
      - { name: "doctrine.orm.entity_listener", entity: App\Entity\Translation\Translatable, event: postLoad }

Entity listener:

namespace App\EventListener;

use App\Entity\Translation\Translatable;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\HttpFoundation\RequestStack;

class LocaleListener
{
    private $currentLocale;
    public function __construct(RequestStack $rs)
    {
        $this->currentLocale = $rs->getCurrentRequest()->getLocale();
        // $this->currentLocale === "en"
    }

    public function postLoad(Translatable $translatable, LifecycleEventArgs $args)
    {
        $translatable->setLocale($this->currentLocale);
        // e.g. url: mywebsite.be/nl
        // Why is $this->currentLocale === "en"?
    }
}

What am I doing wrong?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)