在 Zend Framework 1.* 中,可以根据 Apache 服务器的环境配置来让程序调用不同的设置。
主要用于在不同情况下,调用不同的数据库、不同的警告和错误级别等:例如,在开发环境下调用本机数据库和最低级别的警告和错误提示,在测试环境下调用测试数据库和最低级别的警告和错误提示,在发布环境中调用正式数据库和较高级别的警告和错误提示。
在 ZF 1 中,可以在 Apache 的 SetEnv 指令,配合 ZF 1 的 APPLICATION_ENV 常量,以及项目目录中的 /configs/application.ini 来实现这一目的。
Apache 示例:
点击(此处)折叠或打开
-
<VirtualHost *:80>
-
DocumentRoot "E:/ZF1/public"
-
ServerName .local
-
-
# This should be omitted in the production environment
-
SetEnv APPLICATION_ENV development
-
-
<Directory "E:/ZF1/public">
-
Options Indexes MultiViews FollowSymLinks
-
AllowOverride All
-
Order allow,deny
-
Allow from all
-
</Directory>
-
-
</VirtualHost>
/public/index.php 入口文件示例:
-
<?php
-
-
// Define path to application directory
-
defined('APPLICATION_PATH')
-
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
-
-
// Define application environment
-
defined('APPLICATION_ENV')
-
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
-
-
// Ensure library/ is on include_path
-
set_include_path(implode(PATH_SEPARATOR, array(
-
realpath(APPLICATION_PATH . '/../library'),
-
get_include_path(),
-
)));
-
-
/** Zend_Application */
-
require_once 'Zend/Application.php';
-
-
// Create application, bootstrap, and run
-
$application = new Zend_Application(
-
APPLICATION_ENV,
-
APPLICATION_PATH . '/configs/application.ini'
-
);
-
$application->bootstrap()
-
->run();
/configs/application.ini 配置文件示例:
-
[production]
-
phpSettings.display_startup_errors = 0
-
phpSettings.display_errors = 0
-
includePaths.library = APPLICATION_PATH "/../library"
-
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
-
bootstrap.class = "Bootstrap"
-
appnamespace = "Application"
-
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
-
resources.frontController.params.displayExceptions = 0
-
-
[staging : production]
-
-
[testing : production]
-
phpSettings.display_startup_errors = 1
-
phpSettings.display_errors = 1
-
-
[development : production]
-
phpSettings.display_startup_errors = 1
-
phpSettings.display_errors = 1
-
resources.frontController.params.displayExceptions = 1
在 ZF 2 中,没有提供这一方法,只提供了用本地配置文件替换全局配置文件的方法,即在 /config/autoload 文件夹中可以放置 *global.php 文件,并在其中放置全局配置数组,再放置 *local.php 配置文件,在其中放置本地配置数组。
在载入时,会自动用 local 配置覆盖 global 配置,同时利用 svn 和 git 等版本控制工具,将 *local.php 排除在提交文件之外,以保证本地配置文件不会影响到生产环境和其他人。
但是这种方法有局限性,一是利用了 svn 和 git 的特性,二是只有两级配置。
以下提供一种类似于 ZF 1 的解决方案,思路是根据 Apache 中 SetEnv 指令设置的 APPLICATION_ENV 变量来设置 PHP 的 APPLICATION_ENV 常量,再根据此常量值来载入不同的配置文件。
Apache 示例:
-
Listen 6001
-
NameVirtualHost *:6001
-
-
<Directory "E:/clbx.cn/public">
-
AllowOverride All
-
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
-
<Limit GET POST OPTIONS>
-
Order allow,deny
-
Allow from all
-
</Limit>
-
<LimitExcept GET POST OPTIONS>
-
Order deny,allow
-
Deny from all
-
</LimitExcept>
-
</Directory>
-
-
<VirtualHost *:6001>
-
SetEnv APPLICATION_ENV development
-
ServerAdmin admin@huigu.com
-
DocumentRoot "E:/clbx.cn/public"
-
ServerName local.zlbx.cn
-
ErrorLog "logs/local.zlbx.cn-error.log"
-
CustomLog "logs/local.zlbx.cn-access.log" common
-
</VirtualHost>
-
-
Listen 6002
-
NameVirtualHost *:6002
-
-
<VirtualHost *:6002>
-
SetEnv APPLICATION_ENV local
-
ServerAdmin admin@huigu.com
-
DocumentRoot "E:/clbx.cn/public"
-
ServerName local.zlbx.cn
-
ErrorLog "logs/local.zlbx.cn-error.log"
-
CustomLog "logs/local.zlbx.cn-access.log" common
-
</VirtualHost>
-
-
Listen 6003
-
NameVirtualHost *:6003
-
-
<VirtualHost *:6003>
-
SetEnv APPLICATION_ENV test
-
ServerAdmin admin@huigu.com
-
DocumentRoot "E:/clbx.cn/public"
-
ServerName local.zlbx.cn
-
ErrorLog "logs/local.zlbx.cn-error.log"
-
CustomLog "logs/local.zlbx.cn-access.log" common
-
</VirtualHost>
-
-
Listen 6004
-
NameVirtualHost *:6004
-
-
<VirtualHost *:6004>
-
SetEnv APPLICATION_ENV production
-
ServerAdmin admin@huigu.com
-
DocumentRoot "E:/clbx.cn/public"
-
ServerName local.zlbx.cn
-
ErrorLog "logs/local.zlbx.cn-error.log"
-
CustomLog "logs/local.zlbx.cn-access.log" common
-
</VirtualHost>
注意其中带有 SetEnv APPLICATION_ENV 指令的那些行。这样在带有不同端口时,就会产生不同的 APPLICATION_ENV 变量值。
项目 /public/index.php 入口文件:
-
<?php
-
/**
-
* This makes our life easier when dealing with paths. Everything is relative
-
* to the application root now.
-
*/
-
chdir(dirname(__DIR__));
-
-
// Define application environment
-
defined('APPLICATION_ENV')
-
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
-
-
// Setup autoloading
-
require 'init_autoloader.php';
-
-
// Run the application!
-
Zend\Mvc\Application::init(require 'config/application.config.php')->run();
注意,下面这三行是原文件没有的:
-
// Define application environment
-
defined('APPLICATION_ENV')
-
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
修改 /module/Application/Module.php:
-
<?php
-
return array(
-
'modules' => array(
-
'Application',
-
),
-
-
'module_listener_options' => array(
-
'module_paths' => array(
-
'./module',
-
'./vendor',
-
),
-
-
'config_glob_paths' => array(
-
'config/autoload/{,*.}' . APPLICATION_ENV . '.{global,local}.php', //原为 'config/autoload/{,*.}{global,local}.php',
-
),
-
-
),
-
);
注意第 14 行。
在浏览器中访问 localhost:6001、localhost:6002、localhost:6003、localhost:6004 就会调用不同的配置。
此方案的缺点是不能像 ZF 1 那样,配置之间可以有继承关系,虽然通过修改 /module/Application/Module.php,可以部分实现,总是不够灵活,期待 Zend 官方提供更好的解决方案。
在项目 /config/autoload 文件夹分别建立 development.local.php、test.local.php、test.global.php、production.global.php 文件,分别针对 开发环境、本地测试环境、服务器测试环境、产品环境,内容分别如下:
development.local.php:
-
<?php
-
/**
-
* Global Configuration Override
-
*
-
* You can use this file for overriding configuration values from modules, etc.
-
* You would place values in here that are agnostic to the environment and not
-
* sensitive to security.
-
*
-
* @NOTE: In practice, this file will typically be INCLUDED in your source
-
* control, so do not include passwords or other sensitive information in this
-
* file.
-
*/
-
-
return array(
-
/**
-
* 设置 php 环境。
-
*/
-
'phpSettings' => array(
-
'display_startup_errors' => true,
-
'display_errors' => true,
-
'max_execution_time' => 60,
-
'date.timezone' => 'Asia/Shanghai',
-
'mbstring.internal_encoding' => 'UTF-8',
-
),
-
'db' => array(
-
'driver' => 'Pdo',
-
'dsn' => 'mysql:dbname=development;host=localhost',
-
'driver_options' => array(
-
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
-
),
-
),
-
'service_manager' => array(
-
'factories' => array(
-
'Zend\Db\Adapter\Adapter'
-
=> 'Zend\Db\Adapter\AdapterServiceFactory',
-
),
-
),
-
);
test.local.php:
-
<?php
-
/**
-
* Global Configuration Override
-
*
-
* You can use this file for overriding configuration values from modules, etc.
-
* You would place values in here that are agnostic to the environment and not
-
* sensitive to security.
-
*
-
* @NOTE: In practice, this file will typically be INCLUDED in your source
-
* control, so do not include passwords or other sensitive information in this
-
* file.
-
*/
-
-
return array(
-
/**
-
* 设置 php 环境。
-
*/
-
'phpSettings' => array(
-
'display_startup_errors' => true,
-
'display_errors' => true,
-
'max_execution_time' => 60,
-
'date.timezone' => 'Asia/Shanghai',
-
'mbstring.internal_encoding' => 'UTF-8',
-
),
-
'db' => array(
-
'driver' => 'Pdo',
-
'dsn' => 'mysql:dbname=testlocal;host=localhost',
-
'driver_options' => array(
-
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
-
),
-
),
-
'service_manager' => array(
-
'factories' => array(
-
'Zend\Db\Adapter\Adapter'
-
=> 'Zend\Db\Adapter\AdapterServiceFactory',
-
),
-
),
-
);
test.global.php:
-
<?php
-
/**
-
* Global Configuration Override
-
*
-
* You can use this file for overriding configuration values from modules, etc.
-
* You would place values in here that are agnostic to the environment and not
-
* sensitive to security.
-
*
-
* @NOTE: In practice, this file will typically be INCLUDED in your source
-
* control, so do not include passwords or other sensitive information in this
-
* file.
-
*/
-
-
return array(
-
/**
-
* 设置 php 环境。
-
*/
-
'phpSettings' => array(
-
'display_startup_errors' => true,
-
'display_errors' => true,
-
'max_execution_time' => 60,
-
'date.timezone' => 'Asia/Shanghai',
-
'mbstring.internal_encoding' => 'UTF-8',
-
),
-
'db' => array(
-
'driver' => 'Pdo',
-
'dsn' => 'mysql:dbname=testglobal;host=localhost',
-
'driver_options' => array(
-
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
-
),
-
),
-
'service_manager' => array(
-
'factories' => array(
-
'Zend\Db\Adapter\Adapter'
-
=> 'Zend\Db\Adapter\AdapterServiceFactory',
-
),
-
),
-
);
production.global.php:
-
View Code
-
<?php
-
/**
-
* Global Configuration Override
-
*
-
* You can use this file for overriding configuration values from modules, etc.
-
* You would place values in here that are agnostic to the environment and not
-
* sensitive to security.
-
*
-
* @NOTE: In practice, this file will typically be INCLUDED in your source
-
* control, so do not include passwords or other sensitive information in this
-
* file.
-
*/
-
-
return array(
-
/**
-
* 设置 php 环境。
-
*/
-
'phpSettings' => array(
-
'display_startup_errors' => true,
-
'display_errors' => true,
-
'max_execution_time' => 60,
-
'date.timezone' => 'Asia/Shanghai',
-
'mbstring.internal_encoding' => 'UTF-8',
-
),
-
'db' => array(
-
'driver' => 'Pdo',
-
'dsn' => 'mysql:dbname=production;host=localhost',
-
'driver_options' => array(
-
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
-
),
-
),
-
'service_manager' => array(
-
'factories' => array(
-
'Zend\Db\Adapter\Adapter'
-
=> 'Zend\Db\Adapter\AdapterServiceFactory',
-
),
-
),
-
);
阅读(2607) | 评论(0) | 转发(0) |