当前位置: 首页 > 知识库问答 >
问题:

在Laravel 5.4中禁用请求验证重定向

彭鸿畅
2023-03-14

所以我正在尝试为内部项目开发一个rest API,我遇到了一个问题,当表单请求验证失败时,它会显示@index响应。

所以我有两条路线;

Route::get('/api/clients', 'ClientController@index');
Route::post('/api/clients', 'ClientController@store');

@index列出所有客户端,@store创建一个新客户端,我在@store方法上获得了一个表单请求验证器,该验证器检查为客户端提供的名称。

我想要的是,当验证器失败时,它会显示带有验证错误的JSON响应。但我认为,验证失败了,所以它重定向回同一个页面,但重定向是GET,而不是POST,因此它列出了所有客户端。

我知道您可以设置标题,使其看起来像ajax请求,在其中它将正确显示JSON响应,但我希望它显示JSON响应,而不管它是否为ajax。

我尝试覆盖验证器中的响应方法,但不起作用,我尝试将验证器中的wantsJson方法设置为返回true,但再次不起作用。

非常感谢您的帮助。

代码如下。。。

网状物php

Route::get('/api/clients', 'ClientController@index');
Route::get('/api/clients/{client}', 'ClientController@show');
Route::post('/api/clients', 'ClientController@store');
Route::put('/api/clients/{id}', 'ClientController@update');
Route::delete('/api/clients/{id}', 'ClientController@delete');

ClientController.php

namespace App\Http\Controllers;

use App\Client;
use App\Http\Requests\ClientRequest;

class ClientController extends Controller
{

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ClientRequest $request)
    {
        return Client::create([
            'title'   => request('title'),
            'user_id' => auth()->id()
        ]);
    }

ClientRequest。php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ClientRequest 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'
        ];
    }

    /**
     * Get the failed validation response for the request.
     *
     * @param array $errors
     * @return JsonResponse
     */
     public function response(array $errors)
     {
         dd('exit'); // Doesn't work
     }
}

共有3个答案

薛淮晨
2023-03-14

试试这个

打开应用程序/异常/处理程序。php文件

包括使用

use Illuminate\Validation\ValidationException;

然后添加方法

    /**
     * Create a response object from the given validation exception.
     *
     * @param  \Illuminate\Validation\ValidationException  $e
     * @param  \Illuminate\Http\Request  $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function convertValidationExceptionToResponse(ValidationException $e, $request)
    {
        if ($e->response) {
            return $e->response;
        }

        return response()->json($e->validator->errors()->getMessages(), 422);
    }

现在您可以获得标准验证失败响应,如ajax请求

逑和蔼
2023-03-14

当发出请求时,我们应该发送标题信息。

Accept: application/json
Content-Type: application/json

就是这样,现在laravel不会重定向错误消息并将其作为JSON发送。

马浩淼
2023-03-14

你可以这样试试

在您的申请表中包括“使用优先”如下所示

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

然后

protected function failedValidation(Validator $validator) {
        throw new HttpResponseException(response()->json($validator->errors(), 422));
    }

现在,如果您尝试验证,那么它会像

{
"title": [
"The  title field is required."
]
}
 类似资料:
  • 问题内容: 我正在使用Express框架使用Node.js编写一个小型Web应用程序。我正在使用csrf中间件,但是我想对某些请求禁用它。这就是我在应用程序中添加它的方式: 我想设置没有csrf控件的POST路由。 问题答案: 有几种可能的方法。您基本上需要了解最简单和最正确的规则,以便决定是否使用csrf中间件。如果您大部分时间都希望使用csrf,除了一小部分请求模式白名单,请遵循此答案中我关于

  • 我有一个这样的处理程序和一个自定义注释@validrequest: 注释本身看起来是这样的: 而验证器是这样的: 问题是验证被完全忽略了。我可以发送任何事件有或没有身体和一切工作无一例外。我做的一切都是根据Micronout文档,会有什么问题吗? https://docs.micronaut.io/latest/guide/index.html#BeanValidation

  • 我想用以下值从Ruby代码发送http请求: http://some_domain.com?key=value 我有这个Spring配置: Spring转换配置: 但我有一个错误: 你知道我如何解决这个问题吗?我可以在Spring以某种方式禁用这个CSRF检查吗?

  • 我已经按照这里的建议试过了 但这并没有改变什么。

  • 我需要在爪哇中使用摇摆不定(YAML文件)来验证传入的REST请求。所以任何人都可以帮我这个。提前致谢。

  • 问题内容: 简介: 有没有一种方法可以强制PHP中的内置SoapClient-class通过HTTPS连接到具有无效证书的服务器? 我为什么要这样做? 我已经在没有DNS条目或证书的服务器上部署了新应用程序。我想 在 设置DNS条目和修复证书 之前 尝试使用SoapClient连接到它,而最合理的方法似乎是使客户端在测试过程中忽略证书。 我是否没有意识到这是巨大的安全风险? 这仅用于测试。服务投入