如何更改 Laravel 控制器中的环境配置?
How to change env configuration in Laravel controller?
我想从 Controller 更改 env 配置,但这没有用。
控制器
config(['MAIL_HOST' => 'smtp.sendgrid.net']);
config(['MAIL_PORT' => '25']);
config(['MAIL_USERNAME' => 'apikey']);
config(['MAIL_PASSWORD' => 'SG..']);
Mail::send(
'vendor.maileclipse.templates.news',
["content" => $content],
function ($message) use ($email) {
$message->to($email)->subject('Email');
}
);
}
.env
MAIL_DRIVER=smtp
MAIL_HOST=
MAIL_PORT=
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=xy@xy.com
MAIL_FROM_NAME="Test"
mail.php
您可以像下面这样设置
config([
'mail.mailers.smtp.host' => '',
'mail.mailers.smtp.port' => ,
'mail.mailers.smtp.encryption' => '',
'mail.mailers.smtp.username' => '',
'mail.mailers.smtp.password' => '',
'mail.from.address' => ''
]
);
这将从 mail.php
覆盖
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
注意:正如@ceejayoz 在评论中所告知的那样。请记住,这只会针对该请求进行更改。它不会为其他请求更改它,也不会更新 .env 文件。 –
已更新
config([
'mail.host' => '',
'mail.port' => ,
'mail.encryption' => '',
'mail.username' => '',
'mail.password' => '',
'mail.from.address' => ''
]
);
使用 config
文件键而不是 .env
变量,在你的情况下 config/mail.php
config(['mail.mailers.smtp.host' => 'smtp.sendgrid.net']);
config(['mail.mailers.smtp.port' => '25']);
config(['mail.mailers.smtp.username' => 'apikey']);
config(['mail.mailers.smtp.password' => 'SG...']);
Mail::send(
'vendor.maileclipse.templates.news',
["content" => $content],
function ($message) use ($email) {
$message->to($email)->subject('Email');
}
);
}
我想从 Controller 更改 env 配置,但这没有用。
控制器
config(['MAIL_HOST' => 'smtp.sendgrid.net']);
config(['MAIL_PORT' => '25']);
config(['MAIL_USERNAME' => 'apikey']);
config(['MAIL_PASSWORD' => 'SG..']);
Mail::send(
'vendor.maileclipse.templates.news',
["content" => $content],
function ($message) use ($email) {
$message->to($email)->subject('Email');
}
);
}
.env
MAIL_DRIVER=smtp
MAIL_HOST=
MAIL_PORT=
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=xy@xy.com
MAIL_FROM_NAME="Test"
mail.php
您可以像下面这样设置
config([
'mail.mailers.smtp.host' => '',
'mail.mailers.smtp.port' => ,
'mail.mailers.smtp.encryption' => '',
'mail.mailers.smtp.username' => '',
'mail.mailers.smtp.password' => '',
'mail.from.address' => ''
]
);
这将从 mail.php
覆盖 'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
注意:正如@ceejayoz 在评论中所告知的那样。请记住,这只会针对该请求进行更改。它不会为其他请求更改它,也不会更新 .env 文件。 –
已更新
config([
'mail.host' => '',
'mail.port' => ,
'mail.encryption' => '',
'mail.username' => '',
'mail.password' => '',
'mail.from.address' => ''
]
);
使用 config
文件键而不是 .env
变量,在你的情况下 config/mail.php
config(['mail.mailers.smtp.host' => 'smtp.sendgrid.net']);
config(['mail.mailers.smtp.port' => '25']);
config(['mail.mailers.smtp.username' => 'apikey']);
config(['mail.mailers.smtp.password' => 'SG...']);
Mail::send(
'vendor.maileclipse.templates.news',
["content" => $content],
function ($message) use ($email) {
$message->to($email)->subject('Email');
}
);
}