Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gasparyanyur's avatar

Validate request to api

Hi all. I have a rest full api. So I want to validate via DI my request.

I have a request look like this

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class DownloadRequest 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 [
            'url' => 'required'
        ];
    }
}

In my controller I have a code look like this

public function store(DownloadRequest $request){
       
       // my logic here

   }

But its not working. Also I want to validate my web form request by this DownlaodRequest . Please help me to solve this problem

0 likes
4 replies
tykus's avatar

But its not working

What is not working?

tykus's avatar

Is validation failing in that case? How are you making the request?

If you (temporarily) place the following in your app/Exceptions/Handler.php file's report() method, you will see something useful on validation failure:


if ($exception instanceof \Illuminate\Validation\ValidationException)
{
    return response()->json(['errors' => $exception->errors()]);
}
gasparyanyur's avatar

I have created my request by artisan command, than have injected into the method via DI. So after sending request by Postman I have not got validation errors, I am getting laravel main page. Also your provided code hase not been helpful. I am already getting laravel main page

Please or to participate in this conversation.