图片 url 在开发服务器上工作但在生产服务器上失败
Image url works on development server but fails on production server
我正在使用 Laravel 5.8 版本开发 API 应用程序。当向 products
api 端点发出获取请求时,我 return 一个 ProductResource
集合,看起来像这样
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'category' => $this->category,
'description' => $this->description,
'status' => $this->status,
'price' => $this->price,
'barrels' => $this->barrels,
'interest' => $this->interest,
'start' => $this->start,
'end' => $this->end,
'hidden' => $this->hidden,
'imageUrl' => asset('storage/images/products/' . $this->image->name)
];
}
我遇到的挑战是,在我的本地服务器上单击 returned imageUrl
会显示正确的图像,但在暂存环境中,我找不到默认的 404
页。
我在我正在开发的本地服务器上创建了一个从 public/storage
到 storage/app/public
的符号 link,以便在将应用程序文件上传到暂存环境之前存储实际图像文件.在暂存环境中快速检查 storage/app/public/images/products
会显示图像文件,但我仍然无法从浏览器中查看它。这种行为的可能原因是什么?
这是我的本地和暂存环境中的资源示例
Local/development 服务器
{
"id": 17,
"name": "test",
"category": "test",
"description": "test",
"status": "test",
"price": 10990,
"barrels": 207736,
"interest": 0.2,
"start": "2019-07-25",
"end": "2019-08-25",
"hidden": 0,
"imageUrl": "http://localhost:8000/storage/images/products/pramopro_test_17.jpeg"
}
暂存服务器
{
"id": 13,
"name": "test prod",
"category": "test prod category",
"description": "test prod description",
"status": "loading",
"price": 10000,
"barrels": 300000,
"interest": 0.2,
"start": "2019-07-22",
"end": "2019-08-28",
"hidden": 0,
"imageUrl": "http://staging.pramopro.com/storage/images/products/pramopro_testprod_13.jpeg"
}
所以这个问题是因为我在将应用程序的新副本从我的开发服务器复制到主机 VPS 时忘记将 public/storage
文件夹复制到 public_html
。
您需要为您的存储文件夹创建符号链接
像这样:
ln -s ../project/storage/app/public storage
您将 运行 将其放入您的网站文件中,而不是放入 public_html
文件夹中
我正在使用 Laravel 5.8 版本开发 API 应用程序。当向 products
api 端点发出获取请求时,我 return 一个 ProductResource
集合,看起来像这样
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'category' => $this->category,
'description' => $this->description,
'status' => $this->status,
'price' => $this->price,
'barrels' => $this->barrels,
'interest' => $this->interest,
'start' => $this->start,
'end' => $this->end,
'hidden' => $this->hidden,
'imageUrl' => asset('storage/images/products/' . $this->image->name)
];
}
我遇到的挑战是,在我的本地服务器上单击 returned imageUrl
会显示正确的图像,但在暂存环境中,我找不到默认的 404
页。
我在我正在开发的本地服务器上创建了一个从 public/storage
到 storage/app/public
的符号 link,以便在将应用程序文件上传到暂存环境之前存储实际图像文件.在暂存环境中快速检查 storage/app/public/images/products
会显示图像文件,但我仍然无法从浏览器中查看它。这种行为的可能原因是什么?
这是我的本地和暂存环境中的资源示例
Local/development 服务器
{
"id": 17,
"name": "test",
"category": "test",
"description": "test",
"status": "test",
"price": 10990,
"barrels": 207736,
"interest": 0.2,
"start": "2019-07-25",
"end": "2019-08-25",
"hidden": 0,
"imageUrl": "http://localhost:8000/storage/images/products/pramopro_test_17.jpeg"
}
暂存服务器
{
"id": 13,
"name": "test prod",
"category": "test prod category",
"description": "test prod description",
"status": "loading",
"price": 10000,
"barrels": 300000,
"interest": 0.2,
"start": "2019-07-22",
"end": "2019-08-28",
"hidden": 0,
"imageUrl": "http://staging.pramopro.com/storage/images/products/pramopro_testprod_13.jpeg"
}
所以这个问题是因为我在将应用程序的新副本从我的开发服务器复制到主机 VPS 时忘记将 public/storage
文件夹复制到 public_html
。
您需要为您的存储文件夹创建符号链接 像这样:
ln -s ../project/storage/app/public storage
您将 运行 将其放入您的网站文件中,而不是放入 public_html
文件夹中