Laravel 5.2 queue ignores .env

蹲街弑〆低调 提交于 2019-12-07 19:56:12

问题


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.

  • 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(
    

    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?


回答1:


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


来源:https://stackoverflow.com/questions/41848242/laravel-5-2-queue-ignores-env

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!