Laravel 4.1+ 中的环境检测
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21730630/
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
Environment detection in Laravel 4.1+
提问by Simon Bengtsson
Laravel 4.1 removed the featureto use the domain for detecting what environment the app is running in. Reading the docs they now suggest using host names. However, to me that seems cumbersome if you are working in a team. Should everyone change the bootstrap/start.php
file and add their own host name to be able to run the app in a dev environment? Also, what if you want to have two different environments on the same machine?
Laravel 4.1 删除了使用域检测应用程序运行环境的功能。阅读他们现在建议使用主机名的文档。但是,对我来说,如果您在团队中工作,这似乎很麻烦。每个人都应该更改bootstrap/start.php
文件并添加自己的主机名以便能够在开发环境中运行应用程序吗?另外,如果你想在同一台机器上有两个不同的环境怎么办?
How to best detect the environment if you are working in a team in Laravel 4.1+?
如果您在 Laravel 4.1+ 的团队中工作,如何最好地检测环境?
回答by Miroslav Trninic
Here is my settings from bootstrap/start.phpfile:
这是我在bootstrap/start.php文件中的设置:
$env = $app->detectEnvironment(function() use($app) {
return getenv('ENV') ?: ($app->runningInConsole() ? 'local' : 'production');
});
Instead of default array, this method in my case is returning closure with ternary. That way I got more flexibility in choosing desired environment. You can use switch statement also. Laravel will read return value and configure itself. With getenvnative function, I am just listening for a given environment. If my app is on the server, then it will "pick" server configurations. If locally, then it will pick local (or development) And don't forget to create custom directories for you environemnts in app/configThere is also testing env, which is choosen automatically, every time you are unit testing app.
在我的例子中,这个方法不是默认数组,而是返回带有三元的闭包。这样我就可以更灵活地选择所需的环境。您也可以使用 switch 语句。Laravel 将读取返回值并自行配置。使用getenv本机功能,我只是在监听给定的环境。如果我的应用程序在服务器上,那么它将“选择”服务器配置。如果在本地,那么它会选择本地(或开发)并且不要忘记在app/config 中为您的环境创建自定义目录 还有测试环境,这是每次您对应用程序进行单元测试时自动选择的。
Laravel makes working with environments really fun.
Laravel 让环境工作变得非常有趣。
UPDATE:
更新:
With environments we are mostly cencerned about db credentials.
对于环境,我们主要关注数据库凭据。
For production I use Fortrabbit, so when configuring new app on server, fortrabbit is generating those values for me. I just need to declare them. e.g. DB of just database or db... Or DB_HOST or HOST ... Locally, those values are the one you use for your localhost/mysql settings.
对于生产,我使用Fortrabbit,因此在服务器上配置新应用程序时,fortrabbit 正在为我生成这些值。我只需要声明它们。例如,仅数据库或数据库的 DB... 或 DB_HOST 或 HOST ... 在本地,这些值是您用于 localhost/mysql 设置的值。
回答by Simon Bengtsson
Update:
更新:
In Laravel 5.0 environment detection is no longer needed in the same way. In the .env
file you can simply have a variable for which environment the app should run in.
在 Laravel 5.0 中不再需要以相同的方式进行环境检测。在.env
文件中,您可以简单地设置应用程序应在哪个环境中运行的变量。
Old answer for Laravel <= 4.2
Laravel <= 4.2 的旧答案
What I ended up doing is very close to what carousel suggested. Anyway, thought I would share it. Here is the relevant part of our bootstrap/start.php
file:
我最终做的非常接近 carousel 的建议。无论如何,我想我会分享它。这是我们bootstrap/start.php
文件的相关部分:
$env = $app->detectEnvironment(function ()
{
if($app->runningInConsole())
return "development";
$validEnvironments = array("development", "staging", "production");
if (in_array(getenv('APP_ENV'), $validEnvironments)) {
return getenv('APP_ENV');
}
throw new Exception("Environment variable not set or is not valid. See developer manual for further information.");
});
This way all team members have to declare an environment variable somewhere. I haven't really decided if throwing an exception if the environment variable is not set or just default to production is the best thing. However, with the above, it's easy to change.
这样所有团队成员都必须在某处声明一个环境变量。我还没有真正决定如果环境变量没有设置或者只是默认为生产抛出异常是最好的事情。然而,有了上面的,很容易改变。
回答by alexrussell
For me, I just use 'dev' => '*.local'
and it works. I haven't 100% tested in a team situation but I think it'd work (big assumption alert:) assuming you're on OSX and get the default Alexs-iMac.local
-like hostname.
对我来说,我只是使用'dev' => '*.local'
它并且它有效。我还没有在团队情况下进行 100% 的测试,但我认为它会起作用(大假设警报:)假设您在 OSX 上并获得默认的Alexs-iMac.local
类似主机名。
As for faking an environment, I'm not sure it's really supported. It'll be doable, but in general the whole point of environments is that dev has entirely different needs to production and the two are mutually exclusive. Having the ability to switch on one physical environment seems counter to that goal.
至于伪造环境,我不确定它是否真的受支持。这是可行的,但总的来说,环境的重点是开发人员对生产有完全不同的需求,两者是相互排斥的。能够开启一个物理环境似乎与该目标背道而驰。
回答by Victor BV
Laravel 4.1 and 4.2 detects the environments through the machine names specified in the "bootstrap/start.php" file.
Laravel 4.1 和 4.2 通过“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)。
This way you can regsiter several machines to work as local environment. Other environments can be registered too.
通过这种方式,您可以注册多台机器作为本地环境工作。其他环境也可以注册。
In order to know the current machine name, you can use the following PHP code:
为了知道当前机器名称,可以使用以下PHP代码:
<?php echo gethostname(); ?>
<?php echo gethostname(); ?>
Hope it helps!
希望能帮助到你!
回答by Wallace Maxters
In bootstrap/start.php
define this:
在bootstrap/start.php
定义这个:
$env = $app->detectEnvironment(function() use($app) {
$enviromentsHosts = [
'localhost',
'local',
];
if ($app->runningInConsole() || in_array($app['request']->getHost(), $enviromentsHosts)) {
return 'local';
} else {
return 'production';
}
});
I believe it is better to use only the resources of Laravel 4
我认为最好只使用 Laravel 4
回答by T?n Tam
You can use something like this:
你可以使用这样的东西:
$env = $app->detectEnvironment(function(){
if($_SERVER['HTTP_HOST'] == 'youdomain_local')
{
return 'local';
}elseif($_SERVER['HTTP_HOST'] == 'youdomain_team')
{
return 'team';
}else{
return 'production';
}
});
回答by Wasim A.
what i did is, make dir app/config/local
and use code
我所做的是,制作目录app/config/local
并使用代码
$env = $app->detectEnvironment(function(){
return $_SERVER['HTTP_HOST']=="localhost"?"local":"production";
});
For localhost and online.
对于本地主机和在线。
回答by shazbot
I didn't like that production was default, so I made it anything other than live server will go to local configs:
我不喜欢生产是默认的,所以我把它除了实时服务器之外的任何东西都转到本地配置:
in bootstrap/start.php :
在 bootstrap/start.php 中:
$env = $app->detectEnvironment(function(){
if (gethostname() !== 'live_server_hostname'){
return 'local';
} else {
return 'production';
}
});