Set Application_ENV via virtual host config and read this in PHP
Since SetEnv set's the value to Apache's environment, you can get it with
apache_getenv
— Get an Apache subprocess_env variable
or just
getenv
— Gets the value of an environment variable
If you look at public/index.php
in a ZF project, you will see ZF uses getenv
:
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ?
getenv('APPLICATION_ENV') :
'production'));
An often use alternative would be to read the Hostname from PHP and define the constant accordingly:
if(!defined('APPLICATION_ENV')) {
if(FALSE === stripos($_SERVER['SERVER_NAME'], 'www.yourdomain.com')) {
define(APPLICATION_ENV, 'development');
} else {
define(APPLICATION_ENV, 'production');
}
}
This way, you don't have to rely on the environment setting at all.
Setting environment variables for accessing in PHP when using Apache
Something along the lines:
<VirtualHost hostname:80>
...
SetEnv VARIABLE_NAME variable_value
...
</VirtualHost>
Set APPLICATION_ENV in Zend framework 3
use the correct function ;)
$environment = getenv('APPLICATION_ENV');
Usage for config in ZF2/3:
$env = getenv('APPLICATION_ENV'); // Expect null or "development"
$modules = [];
if ($env === 'development') {
//Array of modules used only for development
$modules = array_merge_recursive($modules, [
'Zf2Whoops',
]);
}
In just Zend Framework nothing special happens with the usage of composer development-enable
. However, if you use Apigility at some point, it will create a development.config.php
file for you which disables application caches.
how to create virtual host on XAMPP
I see two errors:
<VirtualHost *:80> -> Fix to :8081, your POrt the server runs on
ServerName comm-app.local
DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
SetEnv APPLICATION_ENV "development"
<Directory "C:/xampp/htdocs/CommunicationApp/public" -> This is probably why it crashes, missing >
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
-> MIssing close container: </VirtualHost>
Fixed version:
<VirtualHost *:8081>
ServerName comm-app.local
DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
SetEnv APPLICATION_ENV "development"
<Directory "C:/xampp/htdocs/CommunicationApp/public">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
One thing to mention:
You can always try and run command:
service apache2 configtest
This will tell you when you got a malformed configuration and maybe even can tell you where the problem is.
Furthermore it helps avoid unavailability in a LIVE system:
service apache2 restart
will shutdown and then fail to start, this configtest you know beforehand "oops I did something wrong, I should fix this first" but the apache itself is still running with old configuration. :)
Nginx: How to set environment variable in server block
One way to do it is use fastcgi_param
as answered here and here.
I prefered another method. Since I'm using GIT as my VCS:
- I created a file in the root of my project named it "env.conf",
which contains only 1 string ("production", "development", etc) which is the environment name - Ignored this file in .gitignore.
- Read the file and check the value as the first thing in my application starting point.
Yii2 defining environment of console application
If the environment should always be the same for all console apps, it's a good idea to set it in config/console.php
. Just add something like
defined('YII_ENV') or define('YII_ENV', 'console');
When the environment should be different for different machines, gethostname
and a switch
-case
would represent the cleanest way to set the environment.
switch (gethostname()) {
case "machine1": define('YII_ENV', "console-test"); break;
// more case statements
default: define('YII_ENV', "console");
Nginx variables similar to SetEnv in Apache?
NGINX doesn't manage your backend processes like apache does, so it can't affect their environments. To set a new $_SERVER
PHP variable from NGINX, you need to add a new fastcgi_param
entry along with the rest of them. Wherever you're including fastcgi_params
or fastcgi.conf
.
SetEnv APPLICATION_ENV development - .htaccess interacting with Zend Framework?
SetEnv
, used in Apache's configuration (be it a .htaccess file, or a VirtualHost), defines an environment variable.
From PHP, you can read environment variables either :
- using the
getenv()
function. - Or in the
$_SERVER
or$_ENV
superglobal variables
Taking a look at the given index.php
in Zend Frameworks QuickStart, you'll see it uses that environment variable the define the PHP constant called APPLICATION_ENV
:
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: 'production'));
And that constant is later used to initialize the application :
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
Manage two databases
As @Dagon says, usually you detect in which enviroment you are and read one configuration or another.
You can detect it through PHP. You can read the Hostname from PHP and define the constant accordingly:
if(!defined('APPLICATION_ENV')) {
if(FALSE === stripos($_SERVER['SERVER_NAME']), 'www.example.com') {
define(APPLICATION_ENV, 'development');
} else {
define(APPLICATION_ENV, 'production');
}
}
This way, you don't have to rely on the environment setting at all.
Once this has been set (either in your Apache's configuration, or at the system level), you can read its value using the getenv function :
echo getenv('APPLICATION_ENV');
If you want to know more about this you can read this post:
Set Application_ENV via virtual host config and read this in PHP
Related Topics
Update Xampp from Maria Db 10.1 to 10.2
Group MySQL Results by Category and Display Them into Groups Under Each Category
Zf2 + Doctrine 2 - Child-Level Discriminators with Class Table Inheritance
Cannot Pass Null Argument When Using Type Hinting
Comparing Bcrypt Hash Between PHP and Nodejs
Why Are Escape Characters Being Added to the Value of the Hidden Input
How to Show an Alert Box in PHP
Easy Login Script Without Database
Decoding Numeric HTML Entities via PHP
Insert Non-English Decimal Points in MySQL
How to Keep the Chinese or Other Foreign Language as They Are Instead of Converting Them into Codes
How to Send JSON Response in Symfony2 Controller
How to Load View from Alternative Directory in Laravel 4