由 command/schedule 触发时找不到通知图像

Notification Images cannot be found when triggered by an command/schedule

当我通过控制器触发通知时,MailMessage 发送没有问题,包含所有图像。

但是,当通过调用控制台命令的计划项触发相同的通知时,找不到图像。

您没有显示任何代码,但是 所以我想我知道您在说什么。

如果您的 APP_URL 包含目录(例如 http://localhost/myproject),并且您使用 asset() 在视图中引用图像,则生成的引用将在生成视图时正常工作来自浏览器请求,但不是来自队列、计划的作业或一般的 CLI。从 CLI 中,您的 APP_URL 的目录部分在生成的引用中丢失,并且您的图像将被破坏。

这是 Laravel 中来自 Symfony 的已知问题。看起来它刚刚修复,一个月前:https://github.com/laravel/framework/issues/14139

更新

这是该问题的具体示例。我正在使用 Laravel 5.2(尽管根据上面的问题,它已在 8 月底的 5.4 中修复,因此出现在最新版本中)并在用户在网站上执行某些操作时发送交易电子邮件。我更新了我的电子邮件视图以转储一些值。

浏览器请求,实时邮件,Mail::send()QUEUE_DRIVER=sync

env('APP_URL')           http://localhost/myproject                 // OK
config('app.url')        http://localhost/myproject                 // OK
asset('/images/foo.png') http://localhost/myproject/images/foo.png  // OK 

这些值都是正确的,并且 asset() 生成的 URLs 正确显示了我的电子邮件图像。

CLI 请求,排队的邮件,Mail::queue()QUEUE_DRIVER=database

env('APP_URL')           http://localhost/myproject                 // OK
config('app.url')        http://localhost/myproject                 // OK
asset('/images/foo.png') http://localhost/images/foo.png            // xxxx

请注意 asset() 生成的 URL 中缺少 myproject/。我电子邮件中的所有图片引用都已损坏,图片未显示。

如果没有升级到 Laravel 的版本,这个问题已修复,您所能做的就是不要在将从 CLI 生成的任何视图中使用 asset(),而是手动指定资产路径:

config('app.url') . '/images/' . $product->image

旁注——正如@Desh901 在评论中指出的那样,env() should never be used outside of config files:

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files.