我正在浏览Laravel框架,但数据库设置有问题,

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.

即使我的Environment文件夹中有一个database.php配置文件,它从未加载过,我也会在该文件中放入一堆无效字符(键盘混搭),以便让php抛出一个错误,但它从未命中.

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

首先,你需要在paths.php中定义$environments,并将其设置为如下:

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

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

通常情况下,您的文件夹为config,设置为database.phpauth.php

现在只需为你计划使用的每Laravel_Env个文件夹创建一个新文件夹(比如开发).你会得到这样的文件夹 struct ;

/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. 从命令行运行php artisan.您将获得一个包含您的主机名的字符串转储.在我的 case 中是"TSE-Win7";

  3. 删除对artisan.php文件的更改

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

你应该以这样的方式结束:

$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.读这篇文章,在这里回答.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相关问答推荐

我是否需要/是否可以从控制器运行LARLAVEL备份?

如何在 Laravel 中使用 return 停止 Trait php 的执行

如何在生产中的 laravel 应用程序中获取标头请求值?

如何使用 Laravel 查询生成器在 WHERE 条件周围添加括号

在 laravel 4.2 中,用户 'root'@'localhost' 的 Laravel 访问被拒绝(使用密码:YES)

Laravel - 排序的集合输出不是数组

Laravel 表 * 没有名为 * 的列

带有消息...必须返回关系实例的 LogicException.

Laravel 4:读取由 javascript 设置的 cookie

Laravel上传文件无法写入目录

WhereNotExists Laravel Eloquent

如何在 Laravel 5 中按键获取所有缓存项目的列表?

Laravel 存储文件的公共 url

未找到列:1054 未知列 laravel

如何在 laravel 中使用 GROUP_CONCAT

Laravel Mix 生成字体到另一个目录

在 Laravel 5.4 中向多个抄送收件人发送Electron邮件

laravel如何访问具有表编号名称的列?

如何以及在哪里可以使用 laravel 存储图像?

Laravel 排序集合,然后按键