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) }}">
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.
I decided to switch to using Laravel queues to send the emails.
QUEUE_DRIVER
in .env
to database
php artisan queue:table
php artisan migrate
php artisan queue:listen
Changed
\\Illuminate\\Support\\Facades\\Mail::send(
to
\\Illuminate\\Support\\Facades\\Mail::queue(
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'),
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
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.
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.
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?
Thanks to @num8er for finding the solution.
This is a known problem in Laravel that comes from Symfony: https://github.com/laravel/framework/issues/14139
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.