欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Environment Specific Configurations 有大用

环境特定配置

问题

您需要与生产不同的配置。

 

您的应用程序需要不同的设置,具体取决于它是在生产,分期还是在开发环境中运行。

 

使用环境特定的配置。

 

您将需要设置应用程序来检测环境,然后您将能够为每个环境使用其他配置文件。

步骤1 - 更新boostrap / start.php

找到类似于以下内容的行bootstrap/start.php

$ env = $ app - > detectEnvironment array 'local' => array 'your-machine-name' ),));
     

如果机器的主机名为'your-machine-name',上面的代码会将环境设置为本地

 

Laravel 4.1不再检测到Web请求的主机名。这是确定环境的一种不安全的方法,已被弃用。只使用机器的名称(返回gethostname())。

 

更改它以匹配您的机器的主机名。

$ env = $ app - > detectEnvironment array 'local' => array 'mymachine' ),));
     

您可以一次测试多台机器。

$ env = $ app - > detectEnvironment array 'local' => array 'mymachine1' 'mymachine2' ),'staging' => array 'staging-server' ));
      
     

步骤2 - 为您的环境创建一个配置文件夹

$ mkdir app / config / local

步骤3 - 添加自己的配置文件

app/config/local/app.php带有debug on和timezone的示例文件。

<?php
 return array 'debug' => true 'timezone' => 'America / Los_Angeles' ); ?>
     
      

 

讨论

配置如何合并

 

环境特定配置文件中的值被合并到主配置文件中。因此,在上述示例中(假设环境被检测为本地),将发生以下情况。

<?php
 // key来自app / config / app.php。//这将输出一个大的长随机字符串
echo Config :: get 'app.key' );


//时区来自app / config / local / app.php //这将输出“America / Los_Angeles” 
echo Config :: get 'app.timezone' ); ?>

环境特定的配置值将优先。

 

不要使用名称'测试'

testing保留名为的环境进行单元测试。

来自 http://laravel-recipes.com/recipes/27/environment-specific-configurations

 

Environment Specific Configurations

 

Problem

You want a different configuration than production.

 

Your application needs different settings depending on if it's running in production, staging, or in your development environment.

 

Solution

Use environment specific configurations.

 

You'll need to set up your application to detect the environment and then you'll be able to use additional configuration files for each environment.

Step 1 - Update boostrap/start.php

Find the lines that look something like the following within bootstrap/start.php.

$env = $app->detectEnvironment(array(
    'local' => array('your-machine-name'),
));

The code above will set the environment to local if the hostname of the machine is 'your-machine-name'.

 

Laravel 4.1 no longer detects the hostname of the web request. That was an insecure method of determining the environment and has been deprecated. Only the machine's name (as returned by gethostname()) is used.

 

Change this to match the your machine's hostname.

$env = $app->detectEnvironment(array(
    'local' => array('mymachine'),
));

You can test multiple machines at once.

$env = $app->detectEnvironment(array(
    'local' => array('mymachine1', 'mymachine2'),
    'staging' => array('staging-server'),
));

Step 2 - Create a config folder for your environment

$ mkdir app/config/local

Step 3 - Add your own config files

Example app/config/local/app.php file with debug on and timezone set.

<?php
return array(
    'debug' => true,
    'timezone' => 'America/Los_Angeles',
);
?>
 

Discussion

How configurations are merged.

 

Values from environment specific configuration files are merged into the main configuration files. Thus in the example above (assuming environment is detected as local), the following will happen.

<?php
// key comes from app/config/app.php.
// this will output a big long random string
echo Config::get('app.key');

// timezone comes from app/config/local/app.php
// this will output "America/Los_Angeles"
echo Config::get('app.timezone');
?>

The environment specific configuration values will take precedence.

 

Do not use the name 'testing'

An environment named testing is reserved for doing unit tests.

来自http://laravel-recipes.com/recipes/27/environment-specific-configurations


普通分类: