在命令行上与在浏览器中不同时的laravel环境 [英] laravel enviroment when on command line different to in browser
问题描述
我已经将laravel应用程序部署到了一个弹性的beantalk环境中.该应用程序通过bootstrap/start.php中的这段代码来检测其环境
I have deployed my laravel app to an elastic beanstalk enviroment. This app detects its environment with this piece of code in bootstrap/start.php
$env = $app->detectEnvironment(function () {
return getenv('APP_ENV') ?: 'local';
});
弹性beantalk启动时,它将运行其配置文件,我已设置该配置文件来检测是否设置了参数.在这种情况下,我已经将此参数设置为development,并且配置文件随后基于该参数设置了环境变量
When elastic beanstalk starts it runs its configuration files which I have set up to detect if a parameter is set. in this case I have set this parameter to development and the configuration files then set enviroment variables based on this namely
APP_ENV = development
DB_HOST = rds dev endpoint
DB_NAME = rds dev db name
DB_USERNAME = dev username
DB_PASSWORD = devdb pass
当我在浏览器中访问该应用程序时,它正在按预期方式工作,并且已正确连接到开发数据库.
When I visit the app in the browser it is working as intended and it is correctly connecting to the dev db.
当我进入服务器并尝试运行时
When I ssh into the server and try to run
php artisan migrate
我得到一个pdo异常,没有这样的文件或目录
I get a pdo exception no such file or directory
我跑步时
php artisan env
即使定义了环境变量APP_ENV,也会返回
local.我肯定会定义此变量,因为当浏览器中出现laravel错误时,我可以看到服务器/请求数据中带有值开发的变量以及在环境启动时创建的其他环境变量
local is returned even though the environment variable APP_ENV is defined. I am doubly sure that this variable is defined because when laravel errors in the browser I can see the variable with the value development in the server/request data along with the other environment variables that are created on environment launch
我想念什么?
经过编辑以包含基于antonios答案的个性化解决方案
edited to include personalised solution based on antonios answer
.环境
<?php
if (getenv('PARAM1') === false) {
return false;
} else {
switch (getenv('PARAM1')) {
case 'development':
return array(
'APP_ENV' => 'development',
'DB_HOST' => '***',
'DB_NAME' => '***',
'DB_USERNAME' => '***',
'DB_PASSWORD' => '***',
);
break;
case 'staging':
return array(
'APP_ENV' => 'staging',
'DB_HOST' => '***',
'DB_NAME' => '***',
'DB_USERNAME' => '***',
'DB_PASSWORD' => '***',
);
break;
case 'production':
return array(
'APP_ENV' => 'production',
'DB_HOST' => '***',
'DB_NAME' => '***',
'DB_USERNAME' => '***',
'DB_PASSWORD' => '***',
);
break;
}
}
start.php
start.php
$env = require __DIR__.'/../.environment';
if ($env !== false) {
foreach ($env as $key => $value) {
putenv(sprintf('%s=%s', $key, $value));
}
}
$env = $app->detectEnvironment(function () {
return getenv('APP_ENV') ?: 'local';
});
推荐答案
运行时
php artisan migrate
php在其他用户(而不是Web服务器)下运行,因此您需要:
php runs under a different user, not your web server, so you need to:
php artisan migrate --env=development
编辑
cli和web出现问题是我不使用这些Web服务器env vars的原因之一.这是我在环境中的工作方式:
Having trouble with cli and web is one of the reasons why I don't use those web servers env vars. This is how I do my environments:
在应用程序的根目录中创建一个.environment文件并定义您的环境:
Create a .environment file in the root of your application and define your environment:
<?php
return array(
'APP_ENV' => 'development',
'POSTGRESQL.HOST' => 'localhost',
'POSTGRESQL.DATABASE_NAME' => 'laraveldatabase',
'POSTGRESQL.DATABASE_USER' => 'laraveluser',
'POSTGRESQL.DATABASE_PASSWORD' => '!Bassw0rT',
);
将其添加到您的.gitignore
应用程序文件中,因此您不必冒险将密码发送到Github.
Add it to your .gitignore
application file, so you don't risk having your passwords sent to Github.
在$app->detectEnvironment
之前,将.environment文件加载到PHP环境:
Right before $app->detectEnvironment
, load the .environment file to PHP environment:
foreach(require __DIR__.'/../.environment' as $key => $value)
{
putenv(sprintf('%s=%s', $key, $value));
}
然后您只需要使用它:
$env = $app->detectEnvironment(function () {
return getenv('APP_ENV');
});
它将在任何地方工作
<?php
return array(
'connections' => array(
'postgresql' => array(
'driver' => 'pgsql',
'host' => getenv('POSTGRESQL.HOST'),
'database' => getenv('POSTGRESQL.DATABASE_NAME'),
'username' => getenv('POSTGRESQL.DATABASE_USER'),
'password' => getenv('POSTGRESQL.DATABASE_PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
),
);
请注意,我不这样做
return getenv('APP_ENV') ?: 'local';
因为我希望它在将我的应用程序部署到的每台服务器上失败.
Because I want it to fail on every server I deploy my app to.
这篇关于在命令行上与在浏览器中不同时的laravel环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!