Laravel 4.2 - Environment detection - homestead.yaml vs. start.php

夙愿已清 提交于 2019-12-11 13:03:44

问题


Homestead version: 2.0.7

Laravel version: 4.2.16

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:

$env = $app->detectEnvironment(array(

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

));

homestead.yaml:

variables:
- key: APP_ENV
  value: testing123

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).

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.

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).


回答1:


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

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

or via the helper facade

$environment = App::environment();

This ensures that you don't get different results.


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';
});

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

The php artisan env command should then output the same as App::environment();

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-configuration
Laravel 5.0: http://laravel.com/docs/5.0/configuration#environment-configuration



来源:https://stackoverflow.com/questions/28222045/laravel-4-2-environment-detection-homestead-yaml-vs-start-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!