Laravel 5.2队列忽略.env [英] Laravel 5.2 queue ignores .env

查看:97
本文介绍了Laravel 5.2队列忽略.env的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Laravel 5.2应用程序,当用户购买产品时,该应用程序会发送一些电子邮件.电子邮件视图包含对某些图像的引用,例如:

I have a Laravel 5.2 app which sends a few emails when a user buys a product. The email view includes references to some images, like so:

<img src="{{ asset($purchase->image) }}">

这在我拥有的所有3个环境(本地,暂存和生产)中都可以正常工作. asset()使用每个环境中配置的APP_URL正确地将完全限定的URL构造为适当的图像.

This works fine in all 3 environments I have - local, staging, and production. asset() correctly constructs the fully qualified URLs to the appropriate image, using the configured APP_URLs in each environment.

我决定改用 Laravel队列发送电子邮件.

I decided to switch to using Laravel queues to send the emails.

  • 我将.env中的QUEUE_DRIVER更改为database
  • php artisan queue:table
  • php artisan migrate
  • php artisan queue:listen
  • 已更改

  • I changed the QUEUE_DRIVER in .env to database
  • php artisan queue:table
  • php artisan migrate
  • php artisan queue:listen
  • Changed

\Illuminate\Support\Facades\Mail::send(

\Illuminate\Support\Facades\Mail::queue(

并进行了测试购买.该过程有效,邮件已发送,但是已发送的电子邮件中的图像URL错误.看来我配置的APP_URL没有被拾取.

and made a test purchase. The process works, the mail is sent, but the image URLs in the delivered emails are wrong. It seems that my configured APP_URL is not being picked up.

.env

APP_URL=http://localhost/path/to/app

config/app.php

'url' => env('APP_URL', 'http://localhost'),

asset()在我的电子邮件中生成的URL是:

The URLs asset() generates in my email are:

http://localhost/images/foo.jpg

这是不正确的,它们应该是:

which is incorrect, they should be:

http://localhost/path/to/app/images/foo.jpg

看起来好像没有使用在我的.env中定义的APP_URL队列,所以使用了http://localhost的默认值.不使用队列,相同的代码可以正常工作.

It looks like using queues the APP_URL defined in my .env is not seen, so the default of http://localhost is used. Not using queues, the same code works fine.

我唯一能想到的是,正在处理队列的CLI PHP环境与Apache PHP环境有所不同,但是我无法想象有什么差异会导致.env被忽略.

The only thing I can think of is that the CLI PHP environment which is handling the queue is somehow different from the Apache PHP environment, but I can't imagine what difference would cause .env to be ignored.

我从使用Laravel 4.2的人那里发现了一个类似的问题以前,没有答案.我找到了其他一些类似的参考文献,但没有解决方案.有人看到这个或有什么建议吗?

I found a similar question from someone using Laravel 4.2, 2 years ago, with no answer. I found a few other similar references but no solution. Anyone seen this or have any suggestions?

推荐答案

感谢@ num8er查找解决方案.

Thanks to @num8er for finding the solution.

这是Laravel中来自Symfony的一个已知问题: https://github.com /laravel/framework/issues/14139

This is a known problem in Laravel that comes from Symfony: https://github.com/laravel/framework/issues/14139

一种解决方法是按照注释中的@ num8er进行操作,并在视图中硬编码资产路径,而不要使用asset():

A workaround is to do as @num8er suggested in the comments, and hard-code asset paths in views, rather than use asset():

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

这篇关于Laravel 5.2队列忽略.env的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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