我刚刚安装了Laravel 8,并设置了我的api资源。
但是当我尝试创建/更新记录时,它会将我与302
重定向到主页…
- 这是我的
api.php
:
Route::apiResource('addresses', AddressController::class);
- 在我的
AddressController.php
中,我的store方法:
public function store(CreateAddressRequest $request)
{
return response()->json(Address::create($request->validated()));
}
需要帮助(理解),请。
请设置报头为postman - Accept: application/json.
当您通过store()
方法创建时,您有CreateAddressRequest
类来验证您的输入。当验证失败时,它会重定向到上一页,如果没有上一页,它可以重定向到主页。
对于API,这种行为是不可取的,因为您希望返回错误消息(在JSON中)而不是重定向。
使用验证器返回错误的示例。验证请求时的REST API