Symfony2:在哪里设置用户定义的时区 [英] Symfony2: Where to set a user defined time zone

查看:22
本文介绍了Symfony2:在哪里设置用户定义的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前项目的一个要求是允许用户为他们的帐户选择一个时区,然后在整个站点中将这个时区用于所有与日期/时间相关的功能.

One of my requirements for my current project is to allow a user to choose a time zone for their account, and then use this time zone for all date/time related features throughout the entire site.

在我看来,我有两个选择:

The way I see it, I have two options:

  • 为每个新的 DateTime 传递一个 DateTimeZone 对象到 DateTime 的构造函数
  • 使用 PHP 的 date_default_timezone_set() 设置默认时区

似乎使用 date_default_timezone_set 是可行的方法,但我不确定我应该在哪里设置它.因为时区会因用户而异,并且整个站点都使用 DateTime,所以我需要将其设置在会影响所有页面的位置.

It seems like using date_default_timezone_set is the way to go, but I'm not sure exactly where I should set it. Because the time zone will be different from user to user and DateTime's are used all over the site, I need to set it somewhere that it will affect all pages.

也许我可以编写一个事件监听器,在成功登录后设置它?如果我采用这种方法,它会在所有页面上保持设置还是仅在每个页面上设置?

Maybe I could write an event listener that sets it after a successful login? If I take this approach, will it stay set across all pages or is it only set on a per-page basis?

我很想听听其他人会如何处理这个问题.

I'd love to hear how others would approach this.

推荐答案

是的,你可以使用事件监听器,挂钩 kernel.request 事件.

Yea, you could use an event listener, hooking on the kernel.request event.

这是我的一个项目中的听众:

Here is the listener from one of my projects:

<?php
namespace VendorBundleAppBundleListener;

use SymfonyComponentSecurityCoreSecurityContextInterface;
use DoctrineDBALConnection;
use JMSDiExtraBundleAnnotationService;
use JMSDiExtraBundleAnnotationObserve;
use JMSDiExtraBundleAnnotationInjectParams;
use JMSDiExtraBundleAnnotationInject;

/**
 * @Service
 */
class TimezoneListener
{
    /**
     * @var SymfonyComponentSecurityCoreSecurityContextInterface
     */
    private $securityContext;

    /**
     * @var DoctrineDBALConnection
     */
    private $connection;

    /**
     * @InjectParams({
     *     "securityContext" = @Inject("security.context"),
     *     "connection"      = @Inject("database_connection")
     * })
     *
     * @param SymfonyComponentSecurityCoreSecurityContextInterface $securityContext
     * @param DoctrineDBALConnection $connection
     */
    public function __construct(SecurityContextInterface $securityContext, Connection $connection)
    {
        $this->securityContext = $securityContext;
        $this->connection      = $connection;
    }

    /**
     * @Observe("kernel.request")
     */
    public function onKernelRequest()
    {
        if (!$this->securityContext->isGranted('ROLE_USER')) {
            return;
        }

        $user = $this->securityContext->getToken()->getUser();
        if (!$user->getTimezone()) {
            return;
        }

        date_default_timezone_set($user->getTimezone());
        $this->connection->query("SET timezone TO '{$user->getTimezone()}'");
    }
}

这篇关于Symfony2:在哪里设置用户定义的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆