[英]Laravel REST API route Problem in Resource file
<?php
namespace App\Http\Resources\Product;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'href' => [
'self' => route('reviews.index', $this->id),
]
];
}
}
我的路線來了"href":{"self":"http:\/\/127.0.0.1:8000\/api\/products\/1\/reviews"}
像這樣,我該如何解決? 謝謝你
這是因為 json 響應而發生的。 json 總是對代碼進行編碼,因此您可以使用JSON_UNESCAPED_SLASHES
跳過 url 斜杠
response()->json(..., 200, [], JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
在 laravel eloquent api 資源也有同樣的問題。將 Z466DEEC76ECDF5FCA6D34 選項應用到 yourResponse 的最佳方法是:
public function withResponse($request, $response)
{
$response->setEncodingOptions(JSON_UNESCAPED_SLASHES);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.