[英]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),
]
];
}
}
My Route Is Coming "href":{"self":"http:\/\/127.0.0.1:8000\/api\/products\/1\/reviews"}
like this, How am I gonna solve it?我的路线来了
"href":{"self":"http:\/\/127.0.0.1:8000\/api\/products\/1\/reviews"}
像这样,我该如何解决? thank you谢谢你
It's happening because of json response.这是因为 json 响应而发生的。 json alway encode the code, so You can use
JSON_UNESCAPED_SLASHES
to skip url slashes json 总是对代码进行编码,因此您可以使用
JSON_UNESCAPED_SLASHES
跳过 url 斜杠
response()->json(..., 200, [], JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
In laravel eloquent api resource also have the same problem.The best way to apply json options to your Response or ResponseCollection is:
在 laravel eloquent api 资源也有同样的问题。将 Z466DEEC76ECDF5FCA6D34 选项应用到 yourResponse 的最佳方法是:
public function withResponse($request, $response)
{
$response->setEncodingOptions(JSON_UNESCAPED_SLASHES);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.