按照 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 找到了如下一段代码:
转载请尊重原创、保留相关链接本文来自多宝平台:http://www.mbodb.com
点击(此处)折叠或打开
- <?php
- namespace PhpSettings;
-
- use Zend\EventManager\Event;
-
- class Module
- {
- /**
- * Configure PHP ini settings on the bootstrap event
- * @param Event $e
- */
- public function onBootstrap(Event $e) {
- $app = $e->getParam('application');
- $config = $app->getConfiguration();
- $phpSettings = $config['phpSettings'];
- if($phpSettings) {
- foreach($phpSettings as $key => $value) {
- ini_set($key, $value);
- }
- }
- }
-
- /* The getAutoloaderConfig() and getConfig() methods are left out here
- for brevity, as they are completely standard.
- */
- }
并在配置文件中添加了如下设置:
点击(此处)折叠或打开
- return array(
- 'phpSettings' => array(
- 'display_startup_errors' => false,
- 'display_errors' => false,
- 'max_execution_time' => 60,
- 'date.timezone' => 'Europe/London',
- 'mbstring.internal_encoding' => 'UTF-8',
- ),
- );
不过上面明确说明,这个只适用于 Zend Framework 2 Beta4,我用的是 Zend Framework 2 2.1.5 正式版,有了一些变化。
首先,这个类的命名空间是 Application,而且其中 onBootstrap 方法已经有了内容。
不管它,先粘过来再说,当然也得讲点规矩,别将代码直接粘到 onBootstrap中,代码如下:
点击(此处)折叠或打开
- <?php
- namespace Application;
-
- use Zend\Mvc\ModuleRouteListener;
- use Zend\Mvc\MvcEvent;
-
- class Module
- {
- public function onBootstrap(MvcEvent $e)
- {
- $e->getApplication()->getServiceManager()->get('translator');
- $eventManager = $e->getApplication()->getEventManager();
- $moduleRouteListener = new ModuleRouteListener();
- $moduleRouteListener->attach($eventManager);
- $this->setPhpSettings($e);
- }
-
-
- private function setPhpSettings(MvcEvent $e)
- {
- $config = $e->getApplication()->getConfiguration();
- if (array_key_exists('phpSettings'], $config) && is_array($config['phpSettings'])) {
- foreach ($config['phpSettings'] as $key => $value) {
- ini_set($key, $value);
- }
- }
- }
-
- public function getConfig()
- {
- return include __DIR__ . '/config/module.config.php';
- }
-
- public function getAutoloaderConfig()
- {
- return array(
- 'Zend\Loader\StandardAutoloader' => array(
- 'namespaces' => array(
- __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
- ),
- ),
- );
- }
- }
没有配置项放在作者所说的文件里,而是放在了项目根目录下 confit/autoload 的 local.php 文件中(将 local.php.dist 改名为 local.php)。
浏览,问题依旧。
仔细翻看 ZF2 源代码,发现,没有 getConfiguration 方法,多宝取而代之的是 getConfig 方法,修改代码:
点击(此处)折叠或打开
- <?php
- namespace Application;
-
- use Zend\Mvc\ModuleRouteListener;
- use Zend\Mvc\MvcEvent;
-
- class Module
- {
- public function onBootstrap(MvcEvent $e)
- {
- $e->getApplication()->getServiceManager()->get('translator');
- $eventManager = $e->getApplication()->getEventManager();
- $moduleRouteListener = new ModuleRouteListener();
- $moduleRouteListener->attach($eventManager);
- $this->setPhpSettings($e);
- }
-
-
- private function setPhpSettings(MvcEvent $e)
- {
- $config = $e->getApplication()->getConfig();
- if ($config['phpSettings']) {
- foreach ($config['phpSettings'] as $key => $value) {
- ini_set($key, $value);
- }
- }
- }
-
- public function getConfig()
- {
- return include __DIR__ . '/config/module.config.php';
- }
-
- public function getAutoloaderConfig()
- {
- return array(
- 'Zend\Loader\StandardAutoloader' => array(
- 'namespaces' => array(
- __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
- ),
- ),
- );
- }
- }
浏览,问题解决!