问题
I am trying to set up 2 environment to my laravel 4 project and i just can't id the project environment.
I see in the source code that detectEnvironment in the start.php uses is:
$args = isset($_SERVER['argv']) ? $_SERVER['argv'] : null;
but when i dd (print) the $_SERVER['argv'] its empty (return null on live server and on my local machine).
do i need to set it up someone?
- keep in mind my live server is shared host and i have limited access to config.
- i did open dir in /config/ with database.php and set up everything in start.php:
.
$env = $app->detectEnvironment(array(
'local' => array('*local*'),
'live' => array('*live*')
));
回答1:
This is how I do set my environment:
Create a .environment
file in the root of your application and define your environment and add your sensitive information to it:
<?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',
);
Add it to your .gitignore
file, so you don't risk having your passwords sent to Github or any other of your servers.
Right before $app->detectEnvironment
, in the file bootstrap/start.php
, load your .environment
file to PHP environment:
foreach(require __DIR__.'/../.environment' as $key => $value)
{
putenv(sprintf('%s=%s', $key, $value));
}
And then you just have to use it:
$env = $app->detectEnvironment(function () {
return getenv('APPLICATION_ENV'); // your environment name is in that file!
});
And it will work everywhere, so you don't need to have separate dirs for development and production anymore:
<?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',
),
),
);
Note that I don't set a fallback:
return getenv('APPLICATION_ENV') ?: 'local';
Because, if I don't set the file, I want it to fail on every server I deploy my app to.
Laravel leverages something similar, you create .env
files in the root of your application:
.env.local.php // this file will appear only in your local environment, not in production
.env.staging.php // for staging
.env.php // for production
In those files you add your sensitive information
<?php
return array(
'DB_HOST' => 'localhost',
'DB_USER' => 'root',
'DB_PASS' => '1023809182root@pass',
);
Create your files separated by environment:
app/config/local/database.php app/config/staging/database.php app/config/database.php
An then in your files, or anywhere in your application, you can access your sensitive data via $_ENV or getenv():
$_ENV['DB_HOST']
$_ENV['DB_USER']
$_ENV['DB_PASS']
Example:
'postgresql' => [
'driver' => 'pgsql',
'host' => 'localhost',
'database' => getenv('DB_HOST'),
'username' => getenv('DB_USER'),
'password' => getenv('DB_PASS'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
But you still have to set the environment name, which might be a problem in your case.
Don't forget to add those files to your .gitignore file, so you don't risk sending them to your github.
Documentation: http://laravel.com/docs/configuration#protecting-sensitive-configuration
回答2:
Actually $args = isset($_SERVER['argv']) ? $_SERVER['argv'] : null;
will work only if arguments passed to the script when running from the command line. So, in Laravel
, if you run any artisan
command then all the arguments passed to that command will be available in the $_SERVER['argv']
otherwise it'll be always NULL
.
To check the environment you may use App::environment()
method or App::isLocal()
method to check if local and to set different environments you may use:
$env = $app->detectEnvironment(array(
// replace with your machine/host name, gethostname() will return hostname
'local' => array('your-machine-name')
));
Or you may use something like this:
$env = $app->detectEnvironment(function(){
// SheikhHeera-PC is my machine name
return $_SERVER['HTTP_HOST'] == 'SheikhHeera-PC' ? 'local':'production';
});
Read more on documentation.
来源:https://stackoverflow.com/questions/22673464/laravel-4-detectenvironment-dosent-work