当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

环境 - 一般

优质
小牛编辑
132浏览
2023-12-01

环境的支援帮助 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 有四个预定义环境。你也可以 建立你自己的自订环境。

  • \Fuel::DEVELOPMENT
    开发环境。
  • \Fuel::TEST
    测试环境。
  • \Fuel::STAGING
    临时环境。
  • \Fuel::PRODUCTION
    生产环境。

建立自订环境

要建立一个自订环境,只要使用一个自订字串,例如 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