Talha Tanveer
Back to blog

Laravel

Keeping Laravel controllers thin with actions and form requests

Move validation and business logic out of controllers into small classes that are easy to test and reuse from jobs and commands.

Abstract cover: a request passing through layered blocks

Most Laravel controllers start small. Then a feature request adds a validation rule, another adds an email, a third adds an audit log — and the store() method becomes the place where everything happens. It still works, but it’s harder to read, harder to test and impossible to reuse from a queued job or an Artisan command.

The problem with fat controllers

A controller’s job is to translate an HTTP request into an application call and back into a response. When it also validates, persists, notifies and logs, every change risks breaking something unrelated.

app/Http/Controllers/OrderController.php — before
public function store(Request $request)
{
    $data = $request->validate([
        'product_id' => 'required|exists:products,id',
        'quantity'   => 'required|integer|min:1',
    ]);

    $order = Order::create([...$data, 'user_id' => $request->user()->id]);
    Mail::to($request->user())->send(new OrderPlaced($order));
    activity()->log('order placed');

    return redirect()->route('orders.show', $order);
}

Validation belongs in a form request

Form requests give validation and authorisation their own class. The controller receives data that is already valid, and the rules are easy to find.

app/Http/Requests/StoreOrderRequest.php
class StoreOrderRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'product_id' => ['required', 'exists:products,id'],
            'quantity'   => ['required', 'integer', 'min:1'],
        ];
    }
}

Tip. Keep authorisation in authorize() only when it depends on the request. Broader rules usually read better as policies.

One action, one job

An action is a plain class with a single public method. It holds the business rule, so the same logic can run from a controller, a job or a command.

app/Actions/PlaceOrder.php
class PlaceOrder
{
    public function handle(User $user, array $data): Order
    {
        $order = $user->orders()->create($data);

        Mail::to($user)->send(new OrderPlaced($order));

        return $order;
    }
}

What the controller looks like now

OrderController.php — after
public function store(StoreOrderRequest $request, PlaceOrder $placeOrder)
{
    $order = $placeOrder->handle($request->user(), $request->validated());

    return redirect()->route('orders.show', $order);
}

Testing the pieces

Because the action doesn’t know about HTTP, it can be tested directly — no request, no route, no session. Feature tests still cover the route end to end, but most of the rule-checking moves into fast, focused tests like this one:

tests/Unit/PlaceOrderTest.php (Pest)
it('places an order and emails the customer', function () {
    Mail::fake();
    $user = User::factory()->create();
    $product = Product::factory()->create();

    $order = app(PlaceOrder::class)->handle($user, [
        'product_id' => $product->id,
        'quantity'   => 2,
    ]);

    expect($order->user_id)->toBe($user->id);
    Mail::assertSent(OrderPlaced::class);
});

Watch out. Don’t turn every three-line method into an action. The pattern pays off when logic is reused, or complex enough to deserve its own tests.

When to use it

  • The same operation is triggered from more than one place.
  • A controller method needs more than a screen to read.
  • You want to test a business rule without making an HTTP request.

Start with the controller that hurts most, extract one action, and see whether the team finds it easier to follow. Conventions are worth more when they’re adopted gradually.

Filed under LaravelAll articles

Keep reading