1

当我使用FormRequest扩展类验证请求时遇到问题。因为当收到错误请求时重定向并且我需要验证错误的响应

我在用着:

  • PHP 7.1.1 (cli) (build: Jan 18 2017 18:51:14) (ZTS MSVC14 (Visual C++ 2015) x86) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998 -2017 Zend 技术。
  • Laravel v5.5.2

我的 FormRequest 类:

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/

4

3 回答 3

4

我解决了这个问题。它用于请求中缺少的标头。

Content-Type:application/json
X-Requested-With:XMLHttpRequest
于 2017-09-13T15:46:42.057 回答
0

我在使用 Laravel 5.5.3 时遇到了同样的问题

有很多人建议覆盖自定义FormRequest类中的响应方法,无论如何这似乎不再起作用,因为之前调用了方法failedValidation。此方法抛出Illuminate\Validation\ValidationException

您可以在app/Exceptions/Handler.php中捕获此异常

于 2017-09-13T14:21:16.477 回答
-3

要在 Laravel 中验证 json,请查看 Laravel 文档 https://laravel.com/docs/5.5/validation#available-validation-rules

于 2017-09-12T18:38:01.187 回答