问题
I'm building an application that needs to create a new database, perform migrations and seed db data via a web page.
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"
));
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.
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?
回答1:
This isn't a pleasant way to do it, but it works.
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
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)
);
}
回答2:
--env
is the option to specify application's environment when the application is starting. In other words, if you specify --env
option, Laravel will use your specified environment instead runs a detecting method in environment detecting method.
So, If you run artisan
via CLI with --env
option, In start file, artisan
can detect --env
option from $_SERVER
variable, specify the application environment and run your command.
In contrast, when you call Artisan::call()
, Laravel will resolve the console application class (Illuminate\Console\Application
) and run your command. Because your application was started, then Application
just runs your command without detecting environment. More over, latest version of migration command class use application environment to get a database connection
Therefore, when your call Artisan::call()
the --env
option is completely omitted.
Just my opinion. If you really want to avoid using passthru()
function, you can rename the production database connection name in app/config/database.php
to unique name e.g. production
and set your default database connection to your new name. When you want to migrate production database, just call Artisan::call('migrate', array('--database' => 'production', '--force' => true))
instead of changing the environment.
来源:https://stackoverflow.com/questions/24643449/laravel-4-2-artisancall-ignoring-env-option