Symfony2: Where to set a user defined time zone(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:在哪里设置用户定义的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Symfony2:在哪里设置用户定义的时区
基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01