Environment issue in Laravel

主宰稳场 提交于 2019-12-13 07:17:13

问题


$env = $app->detectEnvironment(array(
    'local' => array('*localhost*'),
    'test'  => array('chan.app'),
));

This is how I set in boostrap/start.php, and I set ip in hosts file

127.0.0.1 localhost
127.0.0.1 chan.app

No matter I type http://localhost/ or http://chan.app, App::environment() always reveal production, therefore I can't change database config for it.


回答1:


Open your CMD (Mac, Linux, and Windows) and type the command:

hostname

this will return the name of your computer. Then use that name:

$env = $app->detectEnvironment(array(
  'local' => array('ComputerName'),
));



回答2:


Because of security issues, the use of url domains in environment is forbidden. Laravel says to use hostnames instead.

This is why I doubt, that Laravel would recognise (detect) your configuration correctly, as both are on the same machine.

From the 4.2 Upgrade Guide:

Environment Detection Updates

For security reasons, URL domains may no longer be used to detect your application environment. These values are easily spoofable and allow attackers to modify the environment for a request. You should convert your environment detection to use machine host names (hostname command on Mac, Linux, and Windows).

EDIT

Let's say, you want a local and a live environment.

1. Create folders for each configuration:

  • Create a folder local & live inside /app/config/
  • Inside each of those folders you create the config file(s) you wish to override from /app/config/,

For example, in your live (production) environment, you don't want to have the debug option activated.

  • Create a file app.php in the /app/config/live folder.
  • Inside, you'll just return the desired options to override, as defined in the original /app/config/app.php.

    return array('debug' => false);

In the local environment, 'debug' would be set to true, for development.

2. Add the environment to the framework:

In your /bootstrap/start.php file:

$env = $app->detectEnvironment(array(
    'local' => array('local-machine-name'),
    'live' => array('yourdoamin.com')
));

That's the important part:

- Development (local): --> machine name

- Production: --> root-url (yourdomain.com) which represents the "machine" name

See the docs about environment config for further information.



来源:https://stackoverflow.com/questions/26879482/environment-issue-in-laravel

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