Laravel-在config / database.php中执行逻辑

时间:2019-04-17 07:11:07

标签: database laravel config

众所周知,在Laravel中,config/database.php是一堆arraysvalues。但是对于某些条件,我需要在其中做一些逻辑,然后设法在config/database.php中做一些简单的逻辑,并且它可以按我想要的方式工作(可以参考下面的代码),但是我想知道这样做是否还可以/可以这样做吗?

如果这不切实际,您能否建议以下获得相同结果的另一种方法?

'connections' => [

    'mysql' => (function(){ 
        $config = [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'live_db'),
            'username' => env('DB_USERNAME', 'myuser'),
            'password' => env('DB_PASSWORD', '2309423234'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => 'gn_',
            //'prefix_indexes' => true,
            'strict' => false,
            'engine' => env('DB_ENGINE', null),
        ]; 

        // do some logic here (and it's working!!)
        // but........
        // is it okay to do like this?
        if(env('DB_WITH_SSLMODE', false)){
            $config += [
                'sslmode' => env('DB_SSLMODE', 'prefer'),
                'options'   => [
                    PDO::MYSQL_ATTR_SSL_CA    => env('DB_OPT_MYSQL_ATTR_SSL_CA', 'C:\wamp64\bin\cloudsql\certificates\server-ca.pem'),
                    PDO::MYSQL_ATTR_SSL_CERT    => env('DB_OPT_MYSQL_ATTR_SSL_CERT', 'C:\wamp64\bin\cloudsql\certificates\client-cert.pem'),
                    PDO::MYSQL_ATTR_SSL_KEY    => env('DB_OPT_MYSQL_ATTR_SSL_KEY', 'C:\wamp64\bin\cloudsql\certificates\client-key.pem'),
                    PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('DB_OPT_MYSQL_ATTR_SSL_VERIFY_SERVER_CERT', false)
                ],
            ];
        }

        return $config;
    })(),

],

3 个答案:

答案 0 :(得分:2)

您可以为此创建服务提供商:

class SSLServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(Kernel $kernel)
    {

    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $config = \Config::get('database.connections.mysql');

        if (env('DB_WITH_SSLMODE', false)) {
            $config += [
                'sslmode' => env('DB_SSLMODE', 'prefer'),
                'options' => [
                    \PDO::MYSQL_ATTR_SSL_CA => env('DB_OPT_MYSQL_ATTR_SSL_CA',
                        'C:\wamp64\bin\cloudsql\certificates\server-ca.pem'),
                    \PDO::MYSQL_ATTR_SSL_CERT => env('DB_OPT_MYSQL_ATTR_SSL_CERT',
                        'C:\wamp64\bin\cloudsql\certificates\client-cert.pem'),
                    \PDO::MYSQL_ATTR_SSL_KEY => env('DB_OPT_MYSQL_ATTR_SSL_KEY',
                        'C:\wamp64\bin\cloudsql\certificates\client-key.pem'),
                    \PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('DB_OPT_MYSQL_ATTR_SSL_VERIFY_SERVER_CERT', false)
                ],
            ];
        }

        \Config::set('database.connections.mysql', $config);

    }

}

并且不要忘记在config / app.php中注册您的服务提供商

App\Providers\SSLServiceProvider::class,

答案 1 :(得分:1)

使用三元运算符怎么办? (几乎就像laravel 5.8中的默认设置一样。)

注意:第一个解决方案不会从配置数组中删除sslmode。如果需要,只需选择第二个选项即可!

1)它应该像这样工作:

'connections' => [
    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'sslmode' => env('DB_WITH_SSLMODE') ? env('DB_SSLMODE','prefer') : null,
        'options' => (extension_loaded('pdo_mysql') && env('DB_WITH_SSLMODE')) ? [
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            PDO::MYSQL_ATTR_SSL_CERT => env('MYSQL_ATTR_SSL_CERT'),
            PDO::MYSQL_ATTR_SSL_KEY => env('MYSQL_ATTR_SSL_KEY'),
            PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('MYSQL_ATTR_SSL_VERIFY_SERVER_CERT')
        ] : [],
    ],
],

2)脏溶液,但有一个条件:

'connections' => [
    'mysql' => array_merge([
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null
    ], (extension_loaded('pdo_mysql') && env('DB_WITH_SSLMODE')) ? [
        'sslmode' => env('DB_SSLMODE', 'prefer'),
        'options' => [
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            PDO::MYSQL_ATTR_SSL_CERT => env('MYSQL_ATTR_SSL_CERT'),
            PDO::MYSQL_ATTR_SSL_KEY => env('MYSQL_ATTR_SSL_KEY'),
            PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('MYSQL_ATTR_SSL_VERIFY_SERVER_CERT')
        ]
    ] : [ 'options' => [] ]),
],

然后将env()中的默认值移动到.env文件中(请注意,我删除了 DB_OPT _ 前缀):

DB_WITH_SSLMODE=true
MYSQL_ATTR_SSL_CA=C:\wamp64\bin\cloudsql\certificates\server-ca.pem
MYSQL_ATTR_SSL_CERT=C:\wamp64\bin\cloudsql\certificates\client-cert.pem
MYSQL_ATTR_SSL_KEY=C:\wamp64\bin\cloudsql\certificates\client-key.pem
MYSQL_ATTR_SSL_VERIFY_SERVER_CERT=false

因此,如果将 DB_WITH_SSLMODE 设置为false,则会返回一个空数组,并且不会启用ssl。

注意::我认为您指定的 sslmode 仅用于PostgreSQL连接,而您的情况并非如此。

答案 2 :(得分:1)

其实你也可以做一些简单的事情:

$database = [
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'live_db'),
            'username' => env('DB_USERNAME', 'myuser'),
            'password' => env('DB_PASSWORD', '2309423234'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => 'gn_',
            //'prefix_indexes' => true,
            'strict' => false,
            'engine' => env('DB_ENGINE', null),
        ]
    ]
];

if(env('DB_WITH_SSLMODE', false)){
    $database['connections']['mysql'] += [
        'sslmode' => env('DB_SSLMODE', 'prefer'),
        'options'   => [
            PDO::MYSQL_ATTR_SSL_CA    => env('DB_OPT_MYSQL_ATTR_SSL_CA', 'C:\wamp64\bin\cloudsql\certificates\server-ca.pem'),
            PDO::MYSQL_ATTR_SSL_CERT    => env('DB_OPT_MYSQL_ATTR_SSL_CERT', 'C:\wamp64\bin\cloudsql\certificates\client-cert.pem'),
            PDO::MYSQL_ATTR_SSL_KEY    => env('DB_OPT_MYSQL_ATTR_SSL_KEY', 'C:\wamp64\bin\cloudsql\certificates\client-key.pem'),
            PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('DB_OPT_MYSQL_ATTR_SSL_VERIFY_SERVER_CERT', false)
        ]
    ];
}

return $database;