如何在 Laravel 4 上设置不同的环境?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14530560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up different environments on Laravel 4?
提问by Benjamin Gonzalez
I am trying to set up multiple environments on a Laravel 4 app, and naturally different Databases depending on which environment we are on.
我正在尝试在 Laravel 4 应用程序上设置多个环境,并且根据我们所处的环境自然而然地设置不同的数据库。
For my local machine, I set up a virtual host for "local.elders.dev"
对于我的本地机器,我为“local.elders.dev”设置了一个虚拟主机
Unfortunately, for some reason the following code is not working.
不幸的是,由于某种原因,以下代码不起作用。
$env = $app->detectEnvironment(array(
'local' => array('http://local.elders.dev'),
));
Maybe I need to run an artisan command or something else. I feel I am close, but not quite there yet !
也许我需要运行工匠命令或其他东西。我觉得我已经接近了,但还没有完全到位!
Thanks to all !
谢谢大家 !
采纳答案by Benjamin Gonzalez
OK ! I just solved the issue... The code was actually working ok ! The problem is that I was using
好的 !我刚刚解决了这个问题......代码实际上工作正常!问题是我正在使用
$_SERVER['DB1_HOST'] //for Pagodabox.
Of course this was not set on my local environment, which pretty much broke the app...
当然,这不是在我的本地环境中设置的,这几乎破坏了应用程序......
I fixed by simply doing :
我通过简单地做来修复:
isset($_SERVER['DB1_HOST']) ? $_SERVER['DB1_HOST'] : '';
Thanks to @jeroen and @theshiftexchange :)
感谢@jeroen 和@theshiftexchange :)
回答by Arne
Another method is to use the name of the folder the project is in. This works in console and web. I found this to be the only reliable way if different environments are hosted on the same server.
另一种方法是使用项目所在文件夹的名称。这适用于控制台和 Web。如果不同的环境托管在同一台服务器上,我发现这是唯一可靠的方法。
$env = $app->detectEnvironment(array(
'staging' => strpos(getcwd(), '/staging')>-1,
'acceptance' => strpos(getcwd(), '/acceptance')>-1,
'production' => strpos(getcwd(), '/production')>-1,
));
A big advantage is that you can move staging to production by simply renaming or copying the project folder without changing files, db records or environment variables.
一个很大的优势是您可以通过简单地重命名或复制项目文件夹而不更改文件、数据库记录或环境变量来将暂存转移到生产中。
回答by FredTheWebGuy
Handling multiple environments on the same machine with Laravel 4x
使用 Laravel 4x 在同一台机器上处理多个环境
What if you wanted to run multiple environments on the same machine with the same name- for example, a stagingand productionAND localenvironment?
如果你想用相同的名字-例如,一个运行在同一台机器上的多个环境升级和生产与当地环境?
There is a better solution for handling environments in Laravel 4x- and it can be done by adding a one liner to you vhosts file- or .htaccess:
在 Laravel 4x 中有一个更好的处理环境的解决方案,可以通过向您的 vhosts 文件或 .htaccess 添加一个 liner 来完成:
Set local environment variable
设置本地环境变量
In vhost or .htaccess add for your local installation, for staging, for example add:
在 vhost 或 .htaccess 添加本地安装,用于暂存,例如添加:
SetEnv LARAVEL_ENV staging
and the same in your production.htaccess or vhost:
在您的生产.htaccess 或 vhost 中也是如此:
SetEnv LARAVEL_ENV production
Then the usual detectEnvironment() function in start.php.
然后是 start.php 中常用的 detectEnvironment() 函数。
$env = $app->detectEnvironment(function()
{
// Default to local if LARAVEL_ENV is not set
return getenv('LARAVEL_ENV') ?: 'local';
});
We didn't forget local... and that's the the cool part-- your installation will default to local if neither environment variable is found in vhost or .htaccess, as they would be found in the other installations.
我们没有忘记本地......这是很酷的部分——如果在 vhost 或 .htaccess 中找不到环境变量,您的安装将默认为本地,因为它们会在其他安装中找到。
回答by wpjmurray
I know this is answered but for others looking for a solution...
我知道这已得到解答,但对于其他正在寻找解决方案的人...
My environment detection setting looks like this:
我的环境检测设置如下所示:
$env = $app->detectEnvironment(array(
// Development
// any machine name with the term "local" will use the local environment
'local' => array('*local*'),
// Stage
// any machine name with the term "stage" will use the stage environment
'stage' => array('*stage*')
// Production
// production is default, so we don't need to specify any detection
));
));
This is handy because it will work on any project as long as I use "local" for development (like "localhost", "localhost:8000", "my.app.local", etc.). Same goes for "stage". And production is default so anything without "local" or "stage" works for production.
这很方便,因为它适用于任何项目,只要我使用“本地”进行开发(如“localhost”、“localhost:8000”、“my.app.local”等)。“舞台”也是如此。生产是默认的,所以任何没有“本地”或“舞台”的东西都适用于生产。
回答by Jeroen
Try replacing it with 'local.elders.dev'
, I'm not 100% sure but it's probably matching hostnames, not full paths.
尝试用 替换它'local.elders.dev'
,我不是 100% 确定,但它可能匹配主机名,而不是完整路径。
回答by Victor BV
Laravel 4 detects the environments through the machine names specified in the "bootstrap/start.php" file.
Laravel 4 通过“bootstrap/start.php”文件中指定的机器名称检测环境。
For example, in my case the config becomes:
例如,在我的情况下,配置变为:
$env = $app->detectEnvironment(array(
'local' => array('Victor.local', 'Victor-PC'),
));
This means that Laravel will use the 'local' environment settings for both machines: 'Victor.local' (a Mac) and 'Victor-PC' (Windows).
这意味着 Laravel 将为两台机器使用“本地”环境设置:“Victor.local”(Mac)和“Victor-PC”(Windows)。
In order to know the current machine name, you can use the following PHP code:
为了知道当前机器名称,可以使用以下PHP代码:
<?php echo gethostname(); ?>
<?php echo gethostname(); ?>
For each environment you can create a folder in app/config and replace the desired configuration files and properties.
对于每个环境,您可以在 app/config 中创建一个文件夹并替换所需的配置文件和属性。
Regards!
问候!