【问题标题】:Laravel API Request ValidationLaravel API 请求验证
【发布时间】:2017-10-01 03:32:50
【问题描述】:

你好:我正在用 Laravel 写一个 api。

当我尝试在控制器中使用请求验证时,我得到一个不允许的 405 方法。当我删除请求验证时,一切运行顺利。

这是我的路线:

Route::post('product/create', 'Api\v1\ProductController@create');

这是我的控制器:

<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Requests\CreateProduct;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Handlers\Products;
use Illuminate\Support\MessageBag;

class ProductController extends Controller
{
     /**
     * Create Product.
     */
    public function create(CreateProduct $request)
    {
       echo 'product created...';
    }
}

这是我的请求验证器:

<?php
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreateProduct 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 [
            'title' => 'required',
            'price' => 'required',
            'short_description' => 'required'
         ];
    }

     /**
     * Set custom validation messages.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'title.required' => 'Please enter a title.',
            'price.required' => 'Please enter a price.',
            'short_description.required' => 'Please enter a short description.'
        ];
    }
}

当我从“create”方法中删除“CreateProduct $request”时,一切正常。

如何使用 Laravel 的请求验证进行 api 调用?

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    只需简单地添加“,”

    我认为在这之后一切都会顺利进行

    这就是我使用代码可读性规则的方式

    https://pastebin.com/ivBEf7mT

     public function rules()
         {
             return [
                'title' => 'required',
                'price' => 'required',
                'short_description' => 'required',
             ];
        }
    

    【讨论】:

      【解决方案2】:

      在产品控制器中设置验证。

      public function create(Request $request)
      {
         $rules=array(
                  'title' => 'required',
                  'price' => 'required',
                  'short_description' => 'required'
              );
         $messages=array(
              'title.required' => 'Please enter a title.',
              'price.required' => 'Please enter a price.',
              'short_description.required' => 'Please enter a short description.'
          );
              $validator=Validator::make($request->all(),$rules,$messages);
              if($validator->fails())
              {
                  $messages=$validator->messages();
                  $errors=$messages->all();
                  return $this->respondWithError($errors,500);
              }
          echo 'product created...';
      }
      

      【讨论】:

      • 谢谢,我正在尝试从控制器中删除所有验证,原因是,我的 api 和 web 使用相同的验证,并且我尽可能严格地使用 DRY 方法。因此,我创建了自己的验证类,其中包含返回与请求验证器相同的规则和消息方法。我只是将它们注入控制器并使用 Validator::make() 并且所有验证规则/消息都被排除在控制器之外。
      【解决方案3】:

      将此添加到您的异常中 > handler.php > 渲染方法

      if($exception instanceof \Illuminate\Validation\ValidationException){}
      
      return parent::render($request, $exception);
      

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        正如@oseintow 建议的那样:

        if ($exception instanceof ValidationException) {
            return response()->json(['errors'=>$exception->errors()], JsonResponse::HTTP_UNPROCESSABLE_ENTITY);
        }

        【讨论】:

          【解决方案5】:

          我在使用 PostMan 测试我的 API 时遇到了同样的问题。结果发现我使用了错误的标头(Accept 和 Content-Type 应该是 application/json,但我使用的是 application/x-www-form-urlencoded 作为 Content-Type)。 p>

          在更正自定义请求工作正常之后。

          【讨论】:

            猜你喜欢
            • 2018-09-13
            • 2023-03-14
            • 2017-06-02
            • 2015-09-24
            • 1970-01-01
            • 2017-11-21
            • 2020-01-13
            • 2014-06-03
            • 2017-08-28
            相关资源
            最近更新 更多