【问题标题】:laravel 4 - detectEnvironment dosen't worklaravel 4 - 检测环境不起作用
【发布时间】:2014-05-05 14:11:14
【问题描述】:

我正在尝试为我的 laravel 4 项目设置 2 个环境,但我无法识别项目环境。

我在start.php中的detectEnvironment使用的源代码中看到的是:

    $args = isset($_SERVER['argv']) ? $_SERVER['argv'] : null;

但是当我 dd(打印)$_SERVER['argv'] 时它是空的(在实时服务器和我的本地机器上返回 null)。

我需要有人设置吗?

  • 请记住,我的实时服务器是共享主机,我对配置的访问权限有限。
  • 我确实在 /config/ 中使用 database.php 打开了目录,并在 start.php 中设置了所有内容:

.

$env = $app->detectEnvironment(array(

    'local' => array('*local*'),

    'live' => array('*live*')

));

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    这就是我设置环境的方式:

    在您的应用程序的根目录中创建一个.environment 文件并定义您的环境并向其中添加您的敏感信息:

    <?php
    
    return array(
    
         'APPLICATION_ENV' => 'development', /// this is where you will set your environment
    
         'DB_HOST' => 'localhost',
         'DB_DATABASE_NAME' => 'laraveldatabase',
         'DB_DATABASE_USER' => 'laraveluser',
         'DB_DATABASE_PASSWORD' => '!Bassw0rT',
    
    );
    

    将其添加到您的 .gitignore 文件中,这样您就不会冒险将密码发送到 Github 或您的任何其他服务器。

    就在$app-&gt;detectEnvironment 之前,在文件bootstrap/start.php 中,将您的.environment 文件加载到PHP 环境中:

    foreach(require __DIR__.'/../.environment' as $key => $value) 
    {
        putenv(sprintf('%s=%s', $key, $value));
    }
    

    然后你只需要使用它:

    $env = $app->detectEnvironment(function () {
    
        return getenv('APPLICATION_ENV'); // your environment name is in that file!
    
    });
    

    它可以在任何地方工作,因此您不再需要单独的开发和生产目录:

    <?php
    
    return array(
    
        'connections' => array(
    
             'postgresql' => array(
                  'driver'   => 'pgsql',
                  'host'     => getenv('DB_HOST'),
                  'database' => getenv('DB_DATABASE_NAME'),
                  'username' => getenv('DB_DATABASE_USER'),
                  'password' => getenv('DB_DATABASE_PASSWORD'),
                  'charset'  => 'utf8',
                  'prefix'   => '',
                  'schema'   => 'public',
             ),
    
        ),
    
    );
    

    请注意,我没有设置后备:

    return getenv('APPLICATION_ENV') ?: 'local';
    

    因为,如果我不设置文件,我希望它在我将应用程序部署到的每台服务器上都失败

    Laravel 利用了类似的东西,您在应用程序的根目录中创建 .env 文件:

    .env.local.php    // this file will appear only in your local environment, not in production
    .env.staging.php  // for staging
    .env.php          // for production
    

    在这些文件中添加您的敏感信息

    <?php
    
    return array(
    
        'DB_HOST' => 'localhost',
        'DB_USER' => 'root',
        'DB_PASS' => '1023809182root@pass',
    
    );
    

    创建按环境分隔的文件:

    app/config/local/database.php 应用程序/config/staging/database.php app/config/database.php

    然后在您的文件或应用程序的任何位置,您可以通过 $_ENV 或 getenv() 访问您的敏感数据:

    $_ENV['DB_HOST']
    $_ENV['DB_USER']
    $_ENV['DB_PASS']
    

    例子:

    'postgresql' => [
        'driver'   => 'pgsql',
        'host'     => 'localhost',
        'database' => getenv('DB_HOST'),
        'username' => getenv('DB_USER'),
        'password' => getenv('DB_PASS'),
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ],
    

    但您仍然需要设置环境名称,这可能是您的问题。

    不要忘记将这些文件添加到您的 .gitignore 文件中,这样您就不会冒险将它们发送到您的 github。

    文档:http://laravel.com/docs/configuration#protecting-sensitive-configuration

    【讨论】:

    • 这确实比 Alpha 的答案更有用,因为从命令行(例如 artisan)运行 PHP 脚本时不会填充 $_SERVER
    【解决方案2】:

    实际上,$args = isset($_SERVER['argv']) ? $_SERVER['argv'] : null; 只有在从命令行运行时将参数传递给脚本时才会起作用。因此,在Laravel 中,如果您运行任何artisan 命令,则传递给该命令的所有参数都将在$_SERVER['argv'] 中可用,否则它将始终为NULL

    要检查环境,您可以使用App::environment() 方法或App::isLocal() 方法来检查是否为本地并设置您可能使用的不同环境:

    $env = $app->detectEnvironment(array(
        // replace with your machine/host name, gethostname() will return hostname
        'local' => array('your-machine-name')
    ));
    

    或者你可以使用这样的东西:

    $env = $app->detectEnvironment(function(){
        // SheikhHeera-PC is my machine name
        return $_SERVER['HTTP_HOST'] == 'SheikhHeera-PC' ? 'local':'production';
    });
    

    阅读更多关于documentation

    【讨论】:

    • 我不希望我的机器的主机名包含在版本控制系统中并暴露在 GitHub 等网站上,因此前一种方法不可用。后一种方法将在从命令行运行脚本时发出Undefined index: HTTP_HOST 通知,例如工匠。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 2015-01-01
    • 2014-03-11
    • 1970-01-01
    • 2014-03-10
    • 2017-06-19
    相关资源
    最近更新 更多