Chinaunix首页 | 论坛 | 博客
  • 博客访问: 467182
  • 博文数量: 95
  • 博客积分: 2117
  • 博客等级: 大尉
  • 技术积分: 2301
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-16 21:10
个人简介

辽宁铁岭人,现居大连。1970年生。 先后供职于 中国国际海运网、大连学堂科技、大连华仁视线网络科技有限公司、大连中科海云科技有限公司,任职技术总监。 精通PHP、JAVA、Javascript、HTML、CSS等网络编程技术及Linux操作系统。 精通面向对象编程、设计模式、重构及互联网产品设计。

文章分类

全部博文(95)

文章存档

2013年(31)

2012年(2)

2011年(34)

2010年(25)

2008年(3)

分类: PHP

2013-05-03 09:11:02

按照 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 给力,在  找到了如下一段代码:

点击(此处)折叠或打开

  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.  }

浏览,问题解决!

阅读(2766) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~