laravel 5.5 FormRequest class 正在重定向到我,我需要发送数组错误响应
laravel 5.5 FormRequest class is redirecting to me i need send array errors response
当我使用 FormRequest 扩展 class 验证请求 时遇到问题。 因为正在重定向 当收到错误请求并且我需要一个包含验证错误的响应。
我正在使用:
- PHP 7.1.1 (cli)(内置:2017 年 1 月 18 日 18:51:14)(ZTS MSVC14(Visual C++ 2015)x86)
版权所有 (c) 1997-2017 PHP 集团
Zend Engine v3.1.0,版权所有 (c) 1998-2017 Zend Technologies。
- Laravel v5.5.2.
我的表单请求class:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class BillRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'testfield' => 'required'
];
}
}
我的控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\BillRequest;
use App\Bill;
class BillController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(BillRequest $request)
{
$bills = Bill::paginate(10);
return $bills;
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(BillRequest $request)
{
$bill = new Bill($request->all());
$bill->save();
return response('', 201);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$bill = Bill::find($id);
$bill->customer->person;
$bill->vehicle;
$bill->items;
return response($bill, 200);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(BillRequest $request, $id)
{
$bill = Bill::find($id);
$bill->fill($request->all());
$bill->save();
return response('', 200);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$bill = Bill::find($id);
$bill->delete();
return response('', 204);
}
}
路线(api.php):
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::group(['prefix' => 'admin' ], function () {
Route::resource('bills', 'BillController', [
'only' => ['index', 'update', 'show']
]);
});
最后,带有字段 'testfield' 的响应(在请求中)是带有分页数据的 JSON。但是当我发送没有字段的请求时,然后重定向到 localhost:8000/
要验证 Laravel 中的 json,请查看 Laravel 文档
https://laravel.com/docs/5.5/validation#available-validation-rules
我在使用 Laravel 5.5.3
时遇到同样的问题
有很多人建议在自定义FormRequestclass中覆盖response方法,反正这样不行'自从之前调用方法 failedValidation 以来,它似乎不再起作用。此方法抛出 Illuminate\Validation\ValidationException.
您可以在 app/Exceptions/Handler.php
中捕获此异常
我解决了这个问题。这是针对请求中缺少的 header。
Content-Type:application/json
X-Requested-With:XMLHttpRequest
当我使用 FormRequest 扩展 class 验证请求 时遇到问题。 因为正在重定向 当收到错误请求并且我需要一个包含验证错误的响应。
我正在使用:
- PHP 7.1.1 (cli)(内置:2017 年 1 月 18 日 18:51:14)(ZTS MSVC14(Visual C++ 2015)x86) 版权所有 (c) 1997-2017 PHP 集团 Zend Engine v3.1.0,版权所有 (c) 1998-2017 Zend Technologies。
- Laravel v5.5.2.
我的表单请求class:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class BillRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'testfield' => 'required'
];
}
}
我的控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\BillRequest;
use App\Bill;
class BillController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(BillRequest $request)
{
$bills = Bill::paginate(10);
return $bills;
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(BillRequest $request)
{
$bill = new Bill($request->all());
$bill->save();
return response('', 201);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$bill = Bill::find($id);
$bill->customer->person;
$bill->vehicle;
$bill->items;
return response($bill, 200);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(BillRequest $request, $id)
{
$bill = Bill::find($id);
$bill->fill($request->all());
$bill->save();
return response('', 200);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$bill = Bill::find($id);
$bill->delete();
return response('', 204);
}
}
路线(api.php):
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::group(['prefix' => 'admin' ], function () {
Route::resource('bills', 'BillController', [
'only' => ['index', 'update', 'show']
]);
});
最后,带有字段 'testfield' 的响应(在请求中)是带有分页数据的 JSON。但是当我发送没有字段的请求时,然后重定向到 localhost:8000/
要验证 Laravel 中的 json,请查看 Laravel 文档 https://laravel.com/docs/5.5/validation#available-validation-rules
我在使用 Laravel 5.5.3
时遇到同样的问题有很多人建议在自定义FormRequestclass中覆盖response方法,反正这样不行'自从之前调用方法 failedValidation 以来,它似乎不再起作用。此方法抛出 Illuminate\Validation\ValidationException.
您可以在 app/Exceptions/Handler.php
中捕获此异常我解决了这个问题。这是针对请求中缺少的 header。
Content-Type:application/json
X-Requested-With:XMLHttpRequest