Laravel .env文件中的特定于环境的SSL配置 [英] Environment specific SSL config in Laravel .env file

查看:126
本文介绍了Laravel .env文件中的特定于环境的SSL配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Laravel 5.1,并且有多个环境,有些需要SSL,有些不需要.当我需要SSL时,我在database.php中的配置在mysql驱动程序中需要以下内容:

I am running Laravel 5.1 and have multiple environments, some that require SSL, and some that don't. When I require SSL, my config in database.php requires the following in the mysql driver:

'options'   => [
    PDO::MYSQL_ATTR_SSL_KEY     => env('MYSQL_SSL_KEY'), // /path/to/key.pem
    PDO::MYSQL_ATTR_SSL_CERT    => env('MYSQL_SSL_CERT'), // /path/to/cert.pem
    PDO::MYSQL_ATTR_SSL_CA      => env('MYSQL_SSL_CA'), // /path/to/ca.pem
    PDO::MYSQL_ATTR_SSL_CIPHER  => env('MYSQL_SSL_CIPHER')
]

问题是,当我在不使用SSL(例如本地)的环境中将这些env()变量设置为null时,它会崩溃(白屏等).我不必设置"options"键或将其设置为一个空数组才能使其工作,这将删除env()变量,而该变量随后将无法在SSL环境中使用.

The problem is that when I set these env() vars to null in the environments that don't use SSL (e.g local), it breaks (white screen, etc...). I have to either not set the "options" key or set to to an empty array for it to work, which removes the env() vars which then wouldn't work on the SSL environments.

是否有一种方法可以满足可以同时在SSL和非SSL环境中使用的密钥?

Is there a way to satisfy this key that works for both SSL and non-SSL environments?

推荐答案

您可以定义一个新的环境变量来启用或禁用SSL使用,然后使用三元运算符加载适当的配置.

You can define a new environment variable that enables or disables SSL usage, then use a ternary operator to load the appropriate configuration.

在需要启用数据库SSL的环境中,将其添加到.env文件中:

Add this to your .env file in environments where you need database SSL enabled:

MYSQL_SSL=true // not having this variable defined or being false, will disable SSL

在您的config/database.php文件中,修改options键值,以如下所示加载您的连接:

In your config/database.php file, modify the options key value for your connection to be loaded like this:

'options' => (env('MYSQL_SSL')) ? [
    PDO::MYSQL_ATTR_SSL_KEY    => env('MYSQL_SSL_KEY'),  // /path/to/key.pem
    PDO::MYSQL_ATTR_SSL_CERT   => env('MYSQL_SSL_CERT'), // /path/to/cert.pem
    PDO::MYSQL_ATTR_SSL_CA     => env('MYSQL_SSL_CA'),   // /path/to/ca.pem
    PDO::MYSQL_ATTR_SSL_CIPHER => env('MYSQL_SSL_CIPHER')
] : []

我通常不赞成在配置文件中使用逻辑,但是在这种情况下,可能会产生异常.

I'm usually against using logic in the configuration files, but this is a case where an exception might be made.

这篇关于Laravel .env文件中的特定于环境的SSL配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆