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

najmussaqib's avatar

Laravel request classes for a few rules

I'm writing my API in Laravel. I've created a base request class with my app's reusable business validation logic. Now for individual endpoints, I have to define their specific (mostly 1-2) validation rules. Should I write them in their separate request class which inherits the base request class or should I define those rules in the controller functions itself.

Base request class with reusable rules use Illuminate\Foundation\Http\FormRequest;

class BaseRequest extends FormRequest
{

   // Add your reusable business validation logic here
}

Approach 1: Endpoint specific class which only has one rule

class EndpointController extends Controller
{
    public function store(EndpointRequest $request)
    {
        // The request will automatically be validated based on the rules defined in EndpointRequest
        // Your controller logic goes here
     }
}

Approach 2: Instead of a class for a single rule define it in the endpoint itself

public function store(BaseRequest $request)
    {
        $this->validate($request, $rule);
    }

Which approach suits for an API with hundreds of endoints?

0 likes
2 replies
LaryAI's avatar
Level 58

It is recommended to define the specific validation rules for individual endpoints in their separate request classes, which can inherit the base request class. This approach helps to keep the code organized and maintainable.

By creating separate request classes for each endpoint, you can encapsulate the validation logic specific to that endpoint. This allows for better separation of concerns and makes it easier to understand and maintain the code.

Here's an example of how you can implement this approach:

  1. Create a base request class that contains the reusable business validation logic. This class should extend the Laravel's base request class.
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class BaseRequest extends FormRequest
{
    // Add your reusable business validation logic here
}
  1. Create a separate request class for each endpoint that needs specific validation rules. These classes should extend the base request class.
namespace App\Http\Requests;

class EndpointRequest extends BaseRequest
{
    public function rules()
    {
        return [
            // Define the specific validation rules for this endpoint
        ];
    }
}
  1. In your controller, type-hint the specific request class for each endpoint.
namespace App\Http\Controllers;

use App\Http\Requests\EndpointRequest;

class EndpointController extends Controller
{
    public function store(EndpointRequest $request)
    {
        // The request will automatically be validated based on the rules defined in EndpointRequest
        // Your controller logic goes here
    }
}

By following this approach, you can easily manage and reuse your validation logic across different endpoints while keeping the codebase clean and maintainable.

Snapey's avatar

i would write them out in individual requests classes (share nothing)

If thats a lot of duplication the consider putting the common rules in a trait, but you will need to merge the two arrays

Please or to participate in this conversation.