gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:05:19 27 4
gpt4 key购买 nike

众所周知,在 Laravel 中 config/database.php 是一堆数组。但对于某些情况,我需要在那里做一些逻辑,并且我设法在 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;
})(),

],

最佳答案

使用三元运算符怎么样? (几乎就像 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 连接,但您的情况并非如此。

关于database - Laravel - 在 config/database.php 中执行逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55721852/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com