Why We Enforce Strict Types and Action Classes in Every Laravel 11 Project
Fat controllers are fine for a weekend prototype, but they break down fast once a commercial system crosses 30 endpoints. Here is the exact Action-class pattern we use in production.
Most Laravel codebases start clean. You generate a controller, drop validation inside the method, run two Eloquent queries, send an email, and return a redirect. Six months later, that same controller is 700 lines long, three other endpoints need the same logic, and nobody wants to touch it on a Friday evening.
At RU Digital Services, we banned fat controllers years ago. Every new PHP file starts with declare(strict_types=1);, every write operation goes through a dedicated Form Request, and every piece of business logic lives in a single-purpose Action class under app/Actions/.
What Strict Typing Catches Before Code Reaches Production
PHP's default type coercion hides subtle bugs. Passing "15000" from a form input into a fee calculation works until someone passes an empty string "" or null, which quietly casts to 0. When you store money in integer PKR, a silent cast to zero can mark an unpaid balance as cleared.
<?php
declare(strict_types=1);
namespace App\Actions\Payments;
use App\Enums\PaymentStatus;
use App\Models\Payment;
use Illuminate\Support\Facades\DB;
final class VerifyStudentPaymentAction
{
public function execute(Payment $payment, int $verifierUserId): Payment
{
return DB::transaction(function () use ($payment, $verifierUserId): Payment {
$locked = Payment::whereKey($payment->id)->lockForUpdate()->firstOrFail();
if ($locked->status === PaymentStatus::VERIFIED) {
return $locked;
}
$locked->update([
'status' => PaymentStatus::VERIFIED,
'verified_by' => $verifierUserId,
'verified_at' => now(),
]);
return $locked->fresh();
});
}
}
Why Action Classes Beat Generic "Service" Buckets
When teams move logic out of controllers, their first instinct is to create a StudentService with 25 methods and 1,200 lines. That only moves the mess to a different folder. An Action class does one job and exposes one public method: execute().
- Controllers stay readable: A controller method validates input via a Form Request, checks authorization via a Policy or Gate, calls one Action, and returns a view or redirect.
- Testing gets faster: You can write focused Pest tests against the Action directly without booting full HTTP middleware for every edge case.
- Reuse works across HTTP, CLI, and Queues: The same Action can be invoked from an Admin controller, a scheduled Artisan command, and a queued job with zero duplicated queries.
Pairing Strict Types with Larastan Level 6
Strict types alone catch runtime mismatches, but running Larastan (PHPStan) at Level 6 catches missing return types, nullable relation dereferences, and wrong enum comparisons before you even run your test suite. If a developer forgets to handle a nullable $payment->verified_at, static analysis blocks the commit immediately.
Practical Rules for Your Team
- Put
declare(strict_types=1);at line 3 of every new PHP file. - Use PHP 8.1+ backed Enums for every fixed status column (
InquiryStatus,PaymentStatus,CohortStatus). - Keep controllers under 25 lines per method. If a method starts branching on business rules, extract an Action.
- Never trust client-submitted prices, roles, or ownership IDs—always derive them from the database inside the Action.
This structure adds maybe five extra minutes when building a feature on day one, and saves days of debugging when the application has real paying users six months later.
Need help architecting a Laravel backend, Kotlin Android app, or joining our weekend engineering cohort?