简体   繁体   English

laravel未定义变量:主机

[英]laravel Undefined variable: host

My problem is that I get the following error in MySqlConnector.php. 我的问题是,我在MySqlConnector.php中收到以下错误。

 Undefined variable: host error 

I am using two different connections for both read/write. 我对读/写使用两个不同的连接。 I merged them in config/database.php file depending on environment that system uses. 我将它们合并到config / database.php文件中,具体取决于系统使用的环境。 Here is the mysql connection codes. 这是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,
    ),
),

); );

All I wanted to do is using a different host for reading and another one for writing. 我要做的就是使用另一台主机进行阅读,并使用另一台主机进行写作。 When I use one connection in localhost, I get no error. 当我在本地主机中使用一个连接时,没有任何错误。 But in multiple connections, I get the error. 但是在多个连接中,我得到了错误。 What is the reason for the error? 错误的原因是什么?

Ok I found the problem. 好的,我发现了问题。 It was because of laravel version. 这是因为laravel版本。 I updated laravel 4.0 to 4.1 and problem was solved. 我将laravel 4.0更新为4.1,问题已解决。

just for the record of your environment implementation. 仅用于记录您的环境实现。

You have a build in laravel the environmen detection and loading of specific conf files. 您可以在laravel中构建环境检测和特定conf文件的加载。

you can read here: http://laravel.com/docs/configuration#environment-configuration 您可以在这里阅读: http : //laravel.com/docs/configuration#environment-configuration

but the general idea is to define your environments in the : 但一般的想法是在以下环境中定义您的环境:

$env = $app->detectEnvironment(array(

    'local' => array('your-local-machine-name'),
    'production' => array('your--prod-machine-name'),

));

and than you go to config folder and create inside it a folder foreach environment you have. 然后您转到config文件夹并在其中为每个环境创建一个文件夹。 create a production folder, and than each file you want to override simply copy paste it into the folder and edit the things you want to override. 创建一个生产文件夹,然后将要覆盖的每个文件简单地复制粘贴到该文件夹​​中,然后编辑要覆盖的内容。

implement this, and than check your errors back here again 实施此操作,然后再次在此处检查您的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM