laravel 5.5 FormRequest类重定向到我需要发送数组错误响应

时间:2017-09-12 15:11:26

标签: laravel laravel-validation laravel-request laravel-5.5 laravel-response

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

我正在使用:

  • 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

我的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']
    ]);
});

最后,对字段&#39; testfield&#39;的响应。 (在请求中)是带有数据分页的JSON。但是当我发送没有字段的请求时,然后重定向到 localhost:8000 /

3 个答案:

答案 0 :(得分:2)

我解决了这个问题。这是请求中缺少的标题。

Content-Type:application/json
X-Requested-With:XMLHttpRequest

答案 1 :(得分:0)

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

有很多人建议在自定义 FormRequest 类中覆盖响应方法,无论如何,由于方法<,这似乎不再有效之前调用了em> failedValidation 。此方法抛出 Illuminate \ Validation \ ValidationException

您可以在 app / Exceptions / Handler.php

中捕获此异常

答案 2 :(得分:-2)

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