环境驱动的数据库设置在Laravel? [英] Environment driven database settings in Laravel?

查看:480
本文介绍了环境驱动的数据库设置在Laravel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在移动Laravel框架,但我遇到了数据库设置的麻烦,



具体来说,我有我的环境设置,他们工作很好对于application.php配置文件,然而database.php配置文件似乎没有效果。



即使我有一个database.php配置文件在我的环境文件夹它从来没有加载,我把一堆无效字符(键盘mash)到文件中得到php抛出一个错误,但它从未命中。



Laravel不支持基于环境的数据库设置?

解决方案

您可以通过环境设置数据库设置(和任何其他配置设置) p>

对于Laravel 3(对于Laravel 4和Laravel 5,见下文):

- 您需要在 paths.php 中定义 $ environments ,并将其设置为这样:

  $ environments = array(
'development'=> array('*。dev'),
' '=> array('*。com'),
);

Laravel将自动查找此变量,如果设置,



通常您有一个 config 文件夹,其中包含数据库.php auth.php



现在只需为每个 Laravel_Env 您计划使用(如Development)。您最终会得到这样的文件夹结构;

  / application 
/ config
/ development
database.php
/ production
database.php
application.php
config.php
database.php
...
user_agents.php

你会注意到我只有数据库.php



最后,在你的开发/数据库文件中,你会有一些东西like this;

 <?php 
return array(
'default'=>'mysql '
);

p.s。我只是在目前的3.2.12版本的Laravel上测试这个。



提示:还自动为Artisan设置环境,因此您不必在每个命令行上手动包含环境!要执行此操作:


  1. 您需要知道您正在运行Artisan的主机名。要找出 - 临时编辑根文件夹中的 artisan.php ,并添加 var_dump(gethostname());


  2. 从命令行运行 php artisan 你将得到一个带有你的主机名的字符串转储。在我的示例中为TSE-Win7;


  3. 删除对 artisan.php 文件


  4. 将您的主机名(即TSE-Win7)添加到环境中。


你应该得到这样的结果:

  $ environments = array $ b'development'=> array('*。dev','TSE-Win7'),
'production'=> array('*。com'),
);

Artisan现在将使用您的开发环境运行。如果您部署到实时服务器 - 重新运行这些步骤以获取服务器的主机名(),并且您可以仅为服务器配置特定工匠配置!



对于Laravel 4:



默认环境始终为 production 。但在 start.php 文件中,您可以定义其他环境。

  $ env = $ app- > detectEnvironment(array(
'local'=> array('your-machine-name'),
));

在Linux和Mac上,您可以确定主机名通过类型 hostname 在您的终端 - 它将输出您的计算机的名称。在Windows上,在 routes.php 文件的开头放置 dd(gethostname());



要在应用程序中将当前环境变量作为变量 - 阅读此SO回答这里。 Laravel 4:如何获得环境价值?



对于Laravel 5:



有一个配置文件,名为 .env 根目录。
观看此laracast ,config完全解释。


I am moving over the the Laravel framework, but I am having trouble with the database settings,

Specifically, I have my environments setup, and they are working fine for the application.php config file, however the database.php config file seems to have no effect.

Even if I have a database.php config file in my environments folder it is never loaded, I put a bunch of invalid characters (keyboard mash) into the file to get php to throw an error, but it is never hit.

Does Laravel not support environment based database settings? or am I doing this wrong?

解决方案

You can definitely set database settings (and any other config setting) by environment.

For Laravel 3 (for Laravel 4 and Laravel 5 see below):

Firstly - you need to define $environments in your paths.php and set it to something like this:

$environments = array(
  'development' => array('*.dev'),
  'production' => array('*.com'),
);

Laravel will automatically look for this variable, and if set, will use the associated configuration.

Normally you have a config folder, with settings such as database.php and auth.php

Now just create a new folder for each Laravel_Env you plan to use (such as Development). You'll end up with a folder structure like this;

/application
  /config
    /development
      database.php
    /production
      database.php
    application.php
    config.php
    database.php
    ...
    user_agents.php

You'll note I've only included database.php in each subfolder. Laravel will always load the default config settings first, then override them with any custom configs from the environments setting.

Finally, in your development/database file, you would have something like this;

<?php
 return array(
'default' => 'mysql'
 );

p.s. I just tested this on the current 3.2.12 build of Laravel - and it definitely works.

Bonus Tip: You can also automatically set an environment for Artisan, so you do not have to include the environment manually on each command line! To do this:

  1. You need to know your 'hostname' that you are running Artisan on. To find out - temporarily edit the artisan.php in your root folder, and add var_dump(gethostname()); to line 2 (i.e. above everything).

  2. Run php artisan from the command line. You will get a string dump with your hostname. In my case its "TSE-Win7";

  3. Remove the changes to the artisan.php file

  4. Add your hostname (i.e. "TSE-Win7") to the environments.

You should end up with something like this:

$environments = array(
  'development' => array('*.dev', 'TSE-Win7'),
  'production' => array('*.com'),
);

Artisan will now run using your development environment. If you deploy to a live server - re-run these steps to get the hostname() for the server, and you can configure a specific artisan config just for the server!

For Laravel 4:

The default environment is always production. But in your start.php file you can define additional environments.

 $env = $app->detectEnvironment(array(
   'local' => array('your-machine-name'),
));

On Linux and Mac, you may determine your hostname by type hostname in your terminal - it will output the name of your computer. On Windows put dd(gethostname()); at the beginning of your routes.php file - and run the website once - it will show you the current hostname of your computer.

To get the current environment as a variable in your application - read this SO answer here. Laravel 4: how can I get the environment value?

For Laravel 5:

There is single configuration file, called .env in your root directory. Watch this laracast, config explained fully.

这篇关于环境驱动的数据库设置在Laravel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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