【问题标题】:laravel Undefined variable: hostlaravel 未定义变量:主机
【发布时间】:2014-02-07 18:34:00
【问题描述】:

我的问题是在 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 中使用一个连接时,我没有收到任何错误。但是在多个连接中,我得到了错误。错误的原因是什么?

【问题讨论】:

  • 也有同样的错误。 og.ERROR:异常 'ErrorException' 并在 /var/www/html/domain.com/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php:49 中显示消息“未定义变量:主机”

标签: php mysql laravel environment host


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    仅用于记录您的环境实施。

    您在 laravel 中构建了环境检测和特定 conf 文件的加载。

    你可以在这里阅读:http://laravel.com/docs/configuration#environment-configuration

    但总体思路是在 :

    中定义您的环境
    $env = $app->detectEnvironment(array(
    
        'local' => array('your-local-machine-name'),
        'production' => array('your--prod-machine-name'),
    
    ));
    

    然后转到 config 文件夹并在其中创建一个文件夹 foreach 环境。 创建一个生产文件夹,然后您要覆盖的每个文件只需将其复制粘贴到文件夹中并编辑您要覆盖的内容。

    实现这一点,然后在这里再次检查您的错误

    【讨论】:

    • 我已经试过了。这部分运作良好。当使用两个不同的mysql参数进行读/写时,问题就开始了。在这部分,我得到了错误。
    猜你喜欢
    • 2019-04-29
    • 1970-01-01
    • 2015-05-15
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多