在 Laravel 中设置 production/development 环境
Setting up production/development environment in Laravel
我正与我的一位同事在 GitHub 上合作,这是 Laravel 上的一个项目。现在我做出了一些承诺并向这个人发送了 Git link,他看起来很漂亮并且有以下要说的内容:
The migrations worked fine once I set the environment to development
instead of production. What I'll often do, and you certainly don't
have to, is set the default environment to development and set it to
production only if a variable has been set in the virtual host file or
in .htaccess.
现在我理解了设置默认开发环境的部分,但让我完全摆脱困境的是虚拟主机这个词。
我阅读了Laravel上关于生产环境的文档,但是这个人说的让我有点困惑。有人可以澄清一下吗?
同一个人还向我发送了以下功能:
$env = $app->detectEnvironment(function() {
return getenv('APP_ENV') ?: 'local';
});
我是 Laravel 的新手,非常感谢任何帮助。
环境
当您的朋友向您展示代码时:
$env = $app->detectEnvironment(array(
'local' => array('homestead'),
));
它指的是允许您将主机名 (homestead
) 绑定到特定环境名称 (local
) 的框架部分。
您可以在 bootstrap/start.php
的项目中找到包含此代码的文件。或者看看here on GitHub.
当您为特定主机名指定环境名称时,框架将以不同方式加载配置设置。如果您查看文件夹:
app/config/
您会注意到有一个名为 local
的文件夹。该文件夹包含一些与 app/config/
文件夹根目录中的配置文件相匹配的配置文件。当您在具有您在 start.php 文件中指定的主机名的计算机上启动框架时,它会在应用常规配置版本之前使用您在文件的 local/
版本中提供的任何设置。
这意味着如果您为您的本地环境使用不同的数据库凭据,您可以将它们放在 config/local/database.php
配置中,它们将仅在您的本地环境中启动框架时使用。
你的local
目录下的配置项级联,所以你只需要添加与你的生产环境不同的配置。
您可以通过添加新的环境名称和计算机的主机名来设置不同的环境运行:
$env = $app->detectEnvironment(array(
'local' => array('homestead'),
'testing' => array('testserver')
));
现在我可以创建一个名为 app/config/testing
的文件夹,添加一个新的 database.php
配置文件并为我的测试数据库指定凭据。当从测试服务器启动框架时,将使用 testing
配置文件中的任何详细信息,然后其余配置详细信息将来自默认(生产)环境配置文件。
此外,Laravel 4.2 假定 app/config
作为您的生产环境,因此当您在生产服务器上启动框架时,它将使用位于 app/config
的配置文件中的所有凭据
您可以在 how Laravel handles configurations here 上阅读更多内容。
虚拟机 (Homestead)
您朋友所说的虚拟机,homestead, is the official virtual development environment for Laravel. It's basically a virtual machine that is tuned specifically for working with laravel (though you can use it for non-Laravel projects too). Laracasts has a fee video talking about homestead 可以帮助您填写。
我正与我的一位同事在 GitHub 上合作,这是 Laravel 上的一个项目。现在我做出了一些承诺并向这个人发送了 Git link,他看起来很漂亮并且有以下要说的内容:
The migrations worked fine once I set the environment to development instead of production. What I'll often do, and you certainly don't have to, is set the default environment to development and set it to production only if a variable has been set in the virtual host file or in .htaccess.
现在我理解了设置默认开发环境的部分,但让我完全摆脱困境的是虚拟主机这个词。
我阅读了Laravel上关于生产环境的文档,但是这个人说的让我有点困惑。有人可以澄清一下吗?
同一个人还向我发送了以下功能:
$env = $app->detectEnvironment(function() {
return getenv('APP_ENV') ?: 'local';
});
我是 Laravel 的新手,非常感谢任何帮助。
环境
当您的朋友向您展示代码时:
$env = $app->detectEnvironment(array(
'local' => array('homestead'),
));
它指的是允许您将主机名 (homestead
) 绑定到特定环境名称 (local
) 的框架部分。
您可以在 bootstrap/start.php
的项目中找到包含此代码的文件。或者看看here on GitHub.
当您为特定主机名指定环境名称时,框架将以不同方式加载配置设置。如果您查看文件夹:
app/config/
您会注意到有一个名为 local
的文件夹。该文件夹包含一些与 app/config/
文件夹根目录中的配置文件相匹配的配置文件。当您在具有您在 start.php 文件中指定的主机名的计算机上启动框架时,它会在应用常规配置版本之前使用您在文件的 local/
版本中提供的任何设置。
这意味着如果您为您的本地环境使用不同的数据库凭据,您可以将它们放在 config/local/database.php
配置中,它们将仅在您的本地环境中启动框架时使用。
你的local
目录下的配置项级联,所以你只需要添加与你的生产环境不同的配置。
您可以通过添加新的环境名称和计算机的主机名来设置不同的环境运行:
$env = $app->detectEnvironment(array(
'local' => array('homestead'),
'testing' => array('testserver')
));
现在我可以创建一个名为 app/config/testing
的文件夹,添加一个新的 database.php
配置文件并为我的测试数据库指定凭据。当从测试服务器启动框架时,将使用 testing
配置文件中的任何详细信息,然后其余配置详细信息将来自默认(生产)环境配置文件。
此外,Laravel 4.2 假定 app/config
作为您的生产环境,因此当您在生产服务器上启动框架时,它将使用位于 app/config
的配置文件中的所有凭据
您可以在 how Laravel handles configurations here 上阅读更多内容。
虚拟机 (Homestead)
您朋友所说的虚拟机,homestead, is the official virtual development environment for Laravel. It's basically a virtual machine that is tuned specifically for working with laravel (though you can use it for non-Laravel projects too). Laracasts has a fee video talking about homestead 可以帮助您填写。