Laravel 4.2,Artisan :: call()忽略--env选项 [英] Laravel 4.2, Artisan::call() ignoring --env option
问题描述
我正在构建一个应用程序,该应用程序需要创建一个新的数据库,执行迁移并通过网页种子数据库数据.
I'm building an application that needs to create a new database, perform migrations and seed db data via a web page.
我正在尝试使用Laravel 4.2中的以下代码来实现这一目标.请注意,这是在我设置的控制器中.
I'm trying to achieve this with the following code in Laravel 4.2. Note, this is within a controller I've setup.
Artisan::call("migrate", array(
"--env" => "production"
));
无论我使用"--env"选项传递什么环境,运行迁移的环境都是站点当前正在运行的当前环境. IE.如果我在本地环境中运行,并且运行了上面的命令,它将在本地环境中执行迁移,这不是我想要的.
No matter what environment I pass with the "--env" option, the environment that the migration is run on is the current environment that the site is currently running on. Ie. If I'm running on my local environment, and I run the above, it will execute the migration on the local environment which isn't what I'm looking to do.
如果我从命令行运行等效命令php artisan --env=production migrate
,则会得到想要获得的结果.目前,我已经通过passthru()
克服了这个问题,但是如果可以的话,我想充分利用Artisan的这种外观.
If I run the equivalent command php artisan --env=production migrate
from the command line, I get the results I'm looking to achieve. For the time being, I'm getting past this via passthru()
but I'd like to take advantage of this Artisan facade if I can.
有人知道这是怎么回事吗?
Does anyone know what's going on with this?
推荐答案
这不是一种令人愉快的方法,但它可以工作.
This isn't a pleasant way to do it, but it works.
假设您的Artisan环境基于$_SERVER['HTTP_HOST']
,并且您知道将加载环境的HTTP_HOST,则可以在调用start.php之前手动对其进行设置
Assuming your Artisan environment is based on $_SERVER['HTTP_HOST']
and you know the HTTP_HOST that will load your environment then you can set it manually before calling start.php
我用它来基于Behat概要文件中使用的base_url定义Artisan环境.这样,我可以在运行测试之前配置我的数据库.
I used this to define the Artisan environment based on the base_url I was using in a Behat profile. That way I could configure fixture my database before running tests.
/**
* @BeforeSuite
*/
public static function runFixtures(SuiteEvent $suiteEvent) {
// Get the environment domain
$parameters = $suiteEvent->getContextParameters();
$baseUrl = $parameters['base_url'];
$urlParts = parse_url($baseUrl);
$_SERVER['HTTP_HOST'] = $urlParts['host'];
// Now call start.php
require_once 'bootstrap/start.php';
// Call Artisan
$stream = fopen('php://output', 'w');
Artisan::call(
'migrate:refresh',
[
'--seed' => true,
],
new StreamOutput($stream)
);
}
这篇关于Laravel 4.2,Artisan :: call()忽略--env选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!