环境

环境的支援幫助 FuelPHP 和你的应用程序在环境設定的基础上作出決定。 FuelPHP 本身根據现行的环境, 使用环境設定去載入/覆寫額外的配置設定。

环境和設定

Based on the environment the app is set to, the Config class looks for environment-specific config files. The config class looks for config files in the directory that matches the current environment. This can be helpful if you are working with multiple developers that each use their own database connection configurations. Another helpful use case is when you have a server for testing that should display all PHP errors and warnings and you have a production server that should not display any errors or warning but simply log them to be reviewed by a developer later.

Here is an example to illustrate this:

app/
  config/
    auth.php
    db.php
      development/
        db.php
      staging/
        email.php
      mike_dev/
        db.php
        email.php
      production/
        db.php

When your environment is set to \Fuel::DEVELOPMENT, the settings from db.php are merged with development/db.php. If the environment setting is set to any other than \Fuel::DEVELOPMENT, the extra config file is not loaded. The same goes for any other environment setting you might use.

A real-life example for this is the database configuration. There are no default configuration settings (this is possibly very dangerous). There are only environment-specific config settings.

預定義环境

FuelPHP 有四个預定義环境。你也可以 建立你自己的自訂环境。

建立自訂环境

要建立一个自訂环境,只要使用一个自訂字串,例如 mike_dev 并在 /fuel/app/config/ 中建立一个匹配文件夾。

例如:
如果你有一个名为 Mike 的开发人員,你可以建立一个稱为 mike_dev 的自訂环境。

  1. 在 /fuel/app/config/ 中建立一个稱为 mike_dev 的文件夾。
  2. Place any config files that pertain to Mike in the new config folder, for example, place db.php in /fuel/app/config/mike_dev/ to a load custom database configuration when the mike_dev environment is set.
  3. Follow the instructions on setting your environment below. When setting the environment, instead of using a predefined FuelPHP Environment such as PRODUCTION, use the custom string mike_dev

設定你的环境

There are three ways to set your environment. The first two allow you to set the environment that FuelPHP will use when loading web pages. The third option shows you how to set your environment when using FuelPHP's Oil. Oil does not use the environment you set in the first two options below, it has to be set separately, every time you use Oil.

Set Environment with server environment variables (Recommended)

You can use the server environment variable SetEnv to set the environment your application should run in. Every server has its own envrionment variables. Below includes instructions for known configurations. If your server is not included below, read more about environment variables here.

The variable name FUEL_ENV should be specified in UPPERCASE, the environment name in lowercase.

Apache - Server Configuration
  1. Make sure your apache server configuration loads the extension mod_env
  2. Edit the
    httpd.conf
    file (or if you include virtual host configurations the desired virtual host config file) and add the following code.
    // run this application in production mode
    SetEnv FUEL_ENV production
Apache - User Configuration
  1. Make sure your apache server configuration loads the extension mod_env
  2. Create an
    .htaccess
    file in the
    /public
    directory of your application
  3. Edit the
    .htaccess
    file and add the following code.
    // run this application in production mode
    SetEnv FUEL_ENV production

Note that enabling .htaccess will slow Apache down considerably. If possible, use a server configuration and disable the use of .htaccess!

Nginx
  1. Edit the desired file in
    /etc/nginx/sites-available
    and add the following code.
    # run this application in production mode
      location ~ \.php$ {
        fastcgi_param FUEL_ENV production;
      }

Set Environment with /fuel/app/bootstrap.php

If you are unable to set the environment using the FUEL_ENV server variable, you can manually change the setting in fuel/app/bootstrap.php.

// Inside fuel/app/bootstrap.php

/**
 * Your environment.  Can be set to any of the following:
 *
 * Fuel::DEVELOPMENT
 * Fuel::TEST
 * Fuel::STAGING
 * Fuel::PRODUCTION
 * Any string you want, for example, a developer name (mike_dev)
 *
 */
Fuel::$env = (isset($_SERVER['FUEL_ENV']) ? $_SERVER['FUEL_ENV'] : Fuel::PRODUCTION);

Note that when using this code, the environment variable has precedence when set!

Set Environment when using Oil

當使用一个 *unix 的操作系统,你可以在開始 oil 之前使用 env 命令來定義該變数。

$ env FUEL_ENV=production php oil -v

FuelPHP's Oil does not use the same environment that is set for your application. The environment Oil runs in must be set separately, each time you run an Oil instance. The following instructions tell you how to do so. If you are looking to set the environment for your application, see Set Environment with server environment variables (Recommended) or Set Environment with /fuel/app/bootstrap.php

當使用 Windows 時,这已经被回報可以運作:

C:\> set FUEL_ENV=production && php oil -v