ENGINEERING JOURNAL

Structuring a Laravel API: Controllers, Services, and Where Logic Lives

Structuring a Laravel API so it stays readable: thin controllers, Form Requests for validation, services for business logic, and Resources for responses.

THE ARTICLE 01 — 02

Every Laravel API I have worked on started the same way: a clean controller, a few routes, and a model. Then a new requirement arrived, then another, and six months later the controller was 400 lines long, doing validation, database writes, external API calls, and email sending all in one method. Changing anything meant reading the whole file and hoping nothing else broke. Structuring a Laravel API is mostly about deciding, up front, where each kind of logic lives, so the codebase stays readable when it grows.

The problem with fat controllers

A fat controller is hard to test and hard to reuse. When the same creation logic is needed from a console command, an import job, and an HTTP endpoint, a controller method cannot be called from the other two. The usual result is copy-pasted logic that drifts apart over time. A controller that only handles HTTP concerns avoids this entirely. If a method is longer than about 30 lines, or if it does anything that is not about the request and response, that is the signal to move code out of it.

What belongs in a controller

A controller should do four things and nothing else: accept the request, hand validated data to a service, turn the result into a response, and return the right status code. A thin store method looks like this:

class LeadController extends Controller
{
    public function store(StoreLeadRequest $request, LeadService $service)
    {
        $lead = $service->create($request->validated());

        return response()->json(new LeadResource($lead), 201);
    }
}

There is no validation logic here, no database call, no business rule. The controller is a traffic cop. If you can read it in ten seconds and know exactly what the endpoint does, it is doing its job. Anything that needs a decision belongs somewhere else.

Form Requests: validation lives here

Validation rules do not belong in the controller either. A Form Request class owns them, and it can also authorize the request. This keeps the controller clean and gives every endpoint that accepts the same payload one shared set of rules:

class StoreLeadRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email'],
            'source' => ['nullable', 'string', 'max:100'],
        ];
    }
}

When validation fails, Laravel returns a 422 response automatically, before the controller method runs at all. One practical benefit is consistency: every caller gets the same error shape. Another is that the rules are easy to find and change in one place instead of being buried inside a controller method.

Services: where the business logic goes

A service class holds the actual work: creating records, wrapping related writes in a transaction, dispatching jobs, and calling other systems. The service takes plain data in and returns a model or a result, with no knowledge of HTTP at all:

class LeadService
{
    public function create(array $data): Lead
    {
        return DB::transaction(function () use ($data) {
            $lead = Lead::create($data);
            ProcessNewLead::dispatch($lead);

            return $lead;
        });
    }
}

Because the service knows nothing about requests or responses, it can be called from a controller, a queued job, an Artisan command, or a test, and it behaves the same way each time. That is the real payoff. The transaction is also in the right place: if the job dispatch or a later write fails, the whole operation rolls back instead of leaving half-created data behind. For a fuller look at how queued work fits into this picture, see my post on why Laravel queue jobs fail silently.

Models and API Resources have their own jobs

Models should hold relationships, casts, scopes, and small query helpers, not business workflows. A scope like scopeActive() belongs on the model. A multi-step process that touches several models does not. API Resources are the other half of the response story: they decide which fields a client sees, so the controller never returns a raw model with internal columns exposed. Between a thin controller, a Form Request, a service, and a Resource, each file has one reason to change, and that is the whole point of the structure.

The takeaway

Structuring a Laravel API is not about adding layers for their own sake. It is a simple rule: controllers handle HTTP, Form Requests handle validation, services handle business logic, and Resources shape the response. Apply that rule from the first endpoint and the codebase stays easy to change six months later. Ignore it, and every new feature means wading through a controller that does everything.

Muhammad Aqib ← ALL ARTICLES
KEEP EXPLORING

More ideas.
Better engineering.

Explore the journal ↗