关于php:Artisan无法使用数据库配置文件中的$ _SERVER变量

Artisan unable to use $_SERVER variables from database config file

我目前正在共享主机帐户上部署Laravel项目。这是一个开放项目,并作为公共资源库托管在GitHub上。结果,我在生产环境中使用由database.php配置文件中的.htaccess文件设置的动态变量。这还允许我在主机上使用git pull命令更新部署,这有助于加快工作速度。

database.php文件的内容类似于

1
2
3
$database = $_SERVER['DBNAME'];

$database_user = $_SERVER['DBUSER'];

这就像部署到PagodaBox时所做的一样,并且对于所有应用程序都可以在浏览器中正常运行的应用程序而言,它运行正常,没有任何抱怨。

我遇到的问题是,工匠无法使用这些变量,而是在处理migrate指令时尝试使用我相信的变量来连接数据库。我收到一个错误,指的是工匠尝试使用没有密码的数据库进行连接。我一直在使用--env=production调用artisan并进行了测试,但发现只有在database.php文件具有显式指定的变量而不是环境变量的情况下,它才有效。

有没有一种方法可以使技术人员"查看"这些环境变量?

到目前为止证明对我有用的答案:

http://forums.laravel.io/viewtopic.php?pid=8455

Laravel中环境驱动的数据库设置?


因为Artisan是CLI PHP请求-该请求永远不会命中.htaccess文件-因此,永远不会设置您的变量。

解决方法-您可以在artisan文件本身的第3行中定义变量(在之后)

1
2
$_SERVER['DBNAME'] = 'test';
$_SERVER['DBUSER'] = 'something';

编辑:我刚刚注意到您说这是公共托管在github上的-所以您不想在文件中包含用户名/密码吗? 也许将artisan文件作为.gitignore组的一部分-所以您不推/拉那个文件?


Laravel内置了设置环境变量的功能,因此没有理由在.htaccess中进行设置。 Laravel的内置方式可以与工匠一起轻松地工作。

有关要保护的环境变量,请参阅文档的此部分。

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

报价单:

... create a .env.local.php file within the root of your project [...] The .env.local.php should return an array of key-value pairs, much like a typical Laravel configuration file:

1
2
3
4
<?php
return array(
    'TEST_STRIPE_KEY' => 'super-secret-sauce',
);

All of the key-value pairs returned by this file will automatically be available via the $_ENV and $_SERVER PHP"superglobals". You may now reference these globals from within your configuration files:

1
'key' => $_ENV['TEST_STRIPE_KEY']

Be sure to add the .env.local.php file to your .gitignore file. This will allow other developers on your team to create their own local environment configuration, as well as hide your sensitive configuration items from source control.

Add your private environment variables

1
2
3
4
<?php
return array(
    'MY_SECRET_KEY' => 'super-secret-sauce',
);