Laravel 4.2-环境检测-homestead.yaml与s​​tart.php [英] Laravel 4.2 - Environment detection - homestead.yaml vs. start.php

查看:53
本文介绍了Laravel 4.2-环境检测-homestead.yaml与s​​tart.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

宅基版本:2.0.7

Homestead version: 2.0.7

Laravel版本:4.2.16

Laravel version: 4.2.16

我才刚刚开始学习Laravel,我对start.php和homestead.yaml的环境配置之间的差异感到困惑.这是我所拥有的:

I'm just starting to learn Laravel, and I'm confused about the difference between environment configurations with start.php and homestead.yaml. Here's what I have:

start.php:

start.php:

$env = $app->detectEnvironment(array(

   'local' => array('josh-linux'),
   'production' => array('homestead')

));

homestead.yaml:

homestead.yaml:

variables:
- key: APP_ENV
  value: testing123

如果我在终端中运行"php artisan env",它将显示为"local",如果我将其放入家庭宅装箱并运行"php artisan env",则显示为"production",这就是我的期望. (我只是在其中添加了生产"以测试返回的值).

If I run 'php artisan env' in the terminal it says 'local', and if I ssh into my homestead box and run 'php artisan env' it says 'production', which is what I'd expect. (I just threw 'production' in there to test the value returned).

如果我在hello.php中扔<?php var_dump(getenv('APP_ENV')) ?>并刷新页面,它将显示"testing123",这是homestead.yaml中APP_ENV的设置.

If I throw <?php var_dump(getenv('APP_ENV')) ?> in hello.php and refresh the page, it displays 'testing123', which was the setting for APP_ENV in homestead.yaml.

我只是不知道何时使用每一个?如果在start.php文件中进行了环境检测,那么APP_ENV值的目的是什么,反之亦然?另外,我是否应该在本地"中查找我的机器名称和宅基地盒名称?因为我也不确定检测宅基地"环境的意义. (这是我对VM的初次体验,因此我确定我缺少某些东西.)

I'm just confused with knowing when each one is used? What is the purpose of the APP_ENV value if environment detection is being done in the start.php file, and vice versa? Also, should I have 'local' look for both my machine name and the homestead box name? Because I'm also not sure of the point of detecting the 'homestead' environment. (This is my first experience with VM's so I'm sure there's something I'm missing).

推荐答案

在laravel应用中获取环境的唯一方法应该是

The only way to get the environment in your laravel app should be

$environment = app()->environment();

或通过助手外观

$environment = App::environment();

这可以确保您不会得到不同的结果.

This ensures that you don't get different results.

如果您想使用homestead.yaml环境变量而不是laravel 4的标准start.php,则可以在start.php中将其更改为:

If you like to use the homestead.yaml environment variable instead of the standard start.php of laravel 4 you can change it to this in your start.php:

$env = $app->detectEnvironment(function()
{
    return getenv('APP_ENV') ?: 'production';
});

现在,它会查找APP_ENV变量,如果找不到,则默认为production.

Now it looks for the APP_ENV variable and if not found it defaults to production.

然后php artisan env命令应输出与App::environment();

注意

如果您在laravel 4.2和5.0之间切换,请确保记住环境技术有所改变.请参阅此处以获取更多信息:

Note

If you switch between laravel 4.2 and 5.0 make sure that you have in mind that the environment technique changed a little. See here for mor information:

Laravel 4.2: http://laravel.com/docs/4.2/configuration#environment -配置
Laravel 5.0: http://laravel.com/docs/5.0/configuration#environment-configuration

Laravel 4.2: http://laravel.com/docs/4.2/configuration#environment-configuration
Laravel 5.0: http://laravel.com/docs/5.0/configuration#environment-configuration

这篇关于Laravel 4.2-环境检测-homestead.yaml与s​​tart.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆