Zend Framework 2 时区设置警告问题的解决

按照 Zend 官方文档安装了 Zend Framework,启动之后的默认页显示了如下警告:

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead in E:\clbx.cn\module\Application\view\layout\layout.phtml on line 44

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead in E:\clbx.cn\module\Application\view\layout\layout.phtml on line 44
2013 by Zend Technologies Ltd. All rights reserved.

虽然可以通过 php 的设置屏蔽警告,或者直接在 php.ini 文件中设置 date.timezone,又或者在 index.php 文件中添加如下语句:

1 ini_set('date.timezone', 'Asia/Shanghai');

1 date_default_timezone_set('Asia/Shanghai');

不过我是个配置强迫症患者:尽可能的不动系统设置,并且利用配置文件来完成这样的工作,是我的目标。

当然,此类问题优先要找一找有没有现成的解决方案,百度一番,没有结果,还是 google 给力,在 CONFIGURING PHP INI SETTINGS IN ZF2 找到了如下一段代码:

 1 <?php
 2 namespace PhpSettings;
 3  
 4 use Zend\EventManager\Event;
 5  
 6 class Module
 7 {
 8     /**
 9      * Configure PHP ini settings on the bootstrap event
10      * @param Event $e
11      */
12     public function onBootstrap(Event $e) {
13         $app         = $e->getParam('application');
14         $config      = $app->getConfiguration();
15         $phpSettings = $config['phpSettings'];
16         if($phpSettings) {
17             foreach($phpSettings as $key => $value) {
18                 ini_set($key, $value);
19             }
20         }
21     }
22  
23     /* The getAutoloaderConfig() and getConfig() methods are left out here
24        for brevity, as they are completely standard.
25      */
26 }

并在配置文件中添加了如下设置:

1 return array(
2     'phpSettings'   => array(
3         'display_startup_errors'        => false,
4         'display_errors'                => false,
5         'max_execution_time'            => 60,
6         'date.timezone'                 => 'Europe/London',
7         'mbstring.internal_encoding'    => 'UTF-8',
8     ),
9 );

不过上面明确说明,这个只适用于 Zend Framework 2 Beta4,我用的是 Zend Framework 2 2.1.5 正式版,有了一些变化。

首先,这个类的命名空间是 Application,而且其中 onBootstrap 方法已经有了内容。

不管它,先粘过来再说,当然也得讲点规矩,别将代码直接粘到 onBootstrap中,代码如下:

 1 <?php
 2 namespace Application;
 3 
 4 use Zend\Mvc\ModuleRouteListener;
 5 use Zend\Mvc\MvcEvent;
 6 
 7 class Module
 8 {
 9     public function onBootstrap(MvcEvent $e)
10     {
11         $e->getApplication()->getServiceManager()->get('translator');
12         $eventManager        = $e->getApplication()->getEventManager();
13         $moduleRouteListener = new ModuleRouteListener();
14         $moduleRouteListener->attach($eventManager);
15         $this->setPhpSettings($e);
16     }
17 
18 
19     private function setPhpSettings(MvcEvent $e)
20     {
21         $config = $e->getApplication()->getConfiguration();
22         if (array_key_exists('phpSettings'], $config) && is_array($config['phpSettings'])) {
23             foreach ($config['phpSettings'] as $key => $value) {
24                 ini_set($key, $value);
25             }
26         }
27     }
28 
29     public function getConfig()
30     {
31         return include __DIR__ . '/config/module.config.php';
32     }
33 
34     public function getAutoloaderConfig()
35     {
36         return array(
37             'Zend\Loader\StandardAutoloader' => array(
38                 'namespaces' => array(
39                     __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
40                 ),
41             ),
42         );
43     }
44 }

没有配置项放在作者所说的文件里,而是放在了项目根目录下 confit/autoload 的 local.php 文件中(将 local.php.dist 改名为 local.php)。

浏览,问题依旧。

仔细翻看 ZF2 源代码,发现,没有 getConfiguration 方法,取而代之的是 getConfig 方法,修改代码:

 1 <?php
 2 namespace Application;
 3 
 4 use Zend\Mvc\ModuleRouteListener;
 5 use Zend\Mvc\MvcEvent;
 6 
 7 class Module
 8 {
 9     public function onBootstrap(MvcEvent $e)
10     {
11         $e->getApplication()->getServiceManager()->get('translator');
12         $eventManager        = $e->getApplication()->getEventManager();
13         $moduleRouteListener = new ModuleRouteListener();
14         $moduleRouteListener->attach($eventManager);
15         $this->setPhpSettings($e);
16     }
17 
18 
19     private function setPhpSettings(MvcEvent $e)
20     {
21         $config = $e->getApplication()->getConfig();
22         if ($config['phpSettings']) {
23             foreach ($config['phpSettings'] as $key => $value) {
24                 ini_set($key, $value);
25             }
26         }
27     }
28 
29     public function getConfig()
30     {
31         return include __DIR__ . '/config/module.config.php';
32     }
33 
34     public function getAutoloaderConfig()
35     {
36         return array(
37             'Zend\Loader\StandardAutoloader' => array(
38                 'namespaces' => array(
39                     __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
40                 ),
41             ),
42         );
43     }
44 }

浏览,问题解决!

posted on 2013-04-27 10:20  刘兴伟  阅读(1038)  评论(0编辑  收藏  举报

导航