php - laravel undefined variable : host

标签 php mysql laravel environment host

我的问题是我在 MySqlConnector.php 中收到以下错误。

 Undefined variable: host error 

我使用两个不同的连接进行读/写。我根据系统使用的环境将它们合并到 config/database.php 文件中。这是mysql连接代码。

 <?php

switch($_SERVER['LARAVEL_ENV']){
    case 'production':

        $connections = array(
            'mysql' => array(
                'read' => array(
                    'host'  => '127.0.0.1',
                ),
                'write' => array(
                    'host'  => 'hosturl',
                ),
                'driver'    => 'mysql',
                'database'  => 'app_system',
                'username'  => 'username',
                'password'  => 'password',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            ),

            'mysql2' => array(
                'read' => array(
                    'host'  => '127.0.0.1',
                ),
                'write' => array(
                    'host'  => 'hosturl',
                ),
                'driver'    => 'mysql',
                'database'  => 'app_userdata',
                'username'  => 'username',
                'password'  => 'password',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            )
        );

    break;
    case 'beta':

        $connections = array(
            'mysql' => array(
                'read' => array(
                    'host'  => '127.0.0.1',
                ),
                'write' => array(
                    'host'  => 'hosturl',
                ),
                'driver'    => 'mysql',
                'database'  => 'app_system',
                'username'  => 'username',
                'password'  => 'password',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            ),

            'mysql2' => array(
                'read' => array(
                    'host'  => '127.0.0.1',
                ),
                'write' => array(
                    'host'  => 'hosturl',
                ),
                'driver'    => 'mysql',
                'database'  => 'app_userdata',
                'username'  => 'username',
                'password'  => 'password',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            )
        );

    break;
    case 'development':

        $connections = array(
            'mysql' => array(
                'driver'    => 'mysql',
                'host'      => 'localhost',
                'database'  => 'app_system',
                'username'  => 'root',
                'password'  => 'root',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            ),

            'mysql2' => array(
                'driver'    => 'mysql',
                'host'      => 'localhost',
                'database'  => 'app_userdata',
                'username'  => 'root',
                'password'  => 'root',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            )
        );

    break;
}

 return array(
'fetch'         => PDO::FETCH_CLASS,
'default'       => 'mysql',
'connections'   => $connections,
'migrations'    => 'migrations',
'redis'         => array(
    'cluster' => true,
    'default' => array(
        'host'     => '127.0.0.1',
        'port'     => 6379,
        'database' => 0,
    ),
),

);

我想做的就是使用不同的主机进行读取和使用另一个主机进行写入。当我在 localhost 中使用一个连接时,我没有收到任何错误。但是在多个连接中,我得到了错误。错误原因是什么?

最佳答案

好的,我发现了问题。这是因为laravel版本。我将 laravel 4.0 更新到 4.1,问题解决了。

关于php - laravel undefined variable : host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21202673/

相关文章:

java - 如何使用 wamp 服务器将 BIRT 报告与 php 集成?

php - 如果未分配列表,则不会使用 ActiveCampaign API 列出联系人

php - php调用varchar mysql到html时属性错误

php - laravel 事件不发送到推送器

laravel - 从 Laracasts\Validation 类中静态检索规则

php - PHP\Laravel 中的符号链接(symbolic link)问题

php - 电子邮件激活链接无法按预期工作

mysql - 选择其他列也有重复项的 lead_id

php - laravel Eloquent : filtering rows with month or date

php - 用户名、密码、加盐、加密、哈希——这一切是如何运作的?