我正在使用Laravel 5.8版本开发API应用程序。当对products
api端点进行get请求时,我返回一个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)
];
}
我面临的挑战是,在本地服务器上单击返回的imageUrl
将显示正确的图像,但是在登台环境中,我得到默认的404
未找到页面。
我在本地服务器上创建了从public/storage
到storage/app/public
的符号链接,我正在开发该符号链接以存储实际的图像文件,然后再将应用程序文件上传到暂存环境。在登台环境中快速检查storage/app/public/images/products
会显示图像文件,但是我仍然无法从浏览器中查看它。这种行为的可能原因是什么?
这是我的本地环境和暂存环境中的资源示例
本地/开发服务器
{
"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"
}
答案 0 :(得分:1)
您需要对存储文件夹进行符号链接 像这样:
ln -s ../project/storage/app/public storage
您将其运行到您的网站文件中,而不是运行到public_html
文件夹中
答案 1 :(得分:0)
所以造成此问题的原因是,当我将开发的新副本从开发服务器复制到主机VPS时,我忘记将application/json
文件夹复制到public/storage
。