Skip to content

Lesson 1 of 9 · 22m

REST Contracts and Routing in PHP

Design resources before controllers, model errors as part of the contract, and wire PHP routes without turning routing into a junk drawer.

Most PHP APIs fail socially before they fail technically: the endpoint exists, but nobody can predict its behavior. A good API is a promise. The client should know the resource shape, the error shape, the status codes, and what happens on retry before they write the first integration.

Model resources, not actions

REST works best when URLs name things and HTTP methods describe the operation. /invoices/1042 is a resource; /getInvoice.php?id=1042 is a remote function wearing a URL costume.

GET    /v1/invoices/1042        read one invoice
PATCH  /v1/invoices/1042        update mutable fields
POST   /v1/invoices/1042/void   an operation with side effects that is not simple field mutation
GET    /v1/invoices?status=open filter a collection

The last route is deliberately an action because voiding an invoice is a state transition with audit and permission implications. Use action subresources sparingly, not never.

Keep routing thin

In a framework like Symfony or Laravel, routes should point at small controllers or invokable actions. Routing is not where authorization rules, validation logic, and database queries go to hide.

// routes/api.php
Route::middleware(['auth:sanctum', 'throttle:invoices'])->group(function () {
    Route::get('/v1/invoices/{invoice}', ShowInvoice::class);
    Route::patch('/v1/invoices/{invoice}', UpdateInvoice::class);
    Route::post('/v1/invoices/{invoice}/void', VoidInvoice::class);
});

{invoice} should bind to an actual domain object or fail with a 404. Do not accept a raw integer everywhere and discover in the controller that negative IDs, zero, and strings full of whitespace all reached the database layer.

Status codes are client instructions

Use status codes precisely because they drive client behavior:

  • 200 for successful reads and updates that return a representation.
  • 201 with a Location header when a resource is created.
  • 204 when the operation succeeded and there is intentionally no response body.
  • 400 for malformed input the client can fix.
  • 401 when authentication is absent or invalid; 403 when the caller is authenticated but not allowed.
  • 404 when the resource does not exist or must be hidden from this caller.
  • 409 for state conflicts, such as voiding an already-paid invoice.
  • 422 when syntax is valid JSON but the domain rules reject it.

Idempotence is part of the route contract

A PATCH retried twice should not apply a mutation twice. For operations that create charges, invoices, or user accounts, accept an idempotency key and store the result:

Idempotency-Key: 01J8Z4ZK8A2F7R9N0B7N0Z8WED

The first request performs the operation. A retry with the same key returns the stored response. Without this, a mobile client on a flaky network becomes a duplicate-record generator.

Field notes

  • Never put verbs like delete or create in collection URLs unless the operation truly does not map to HTTP semantics.
  • Decide whether 404 hides unauthorized resources. Sometimes it is correct; sometimes it tells attackers which IDs exist.
  • Normalize trailing slashes and route casing once at the edge. Do not let /Invoices/1 and /invoices/1/ become three different endpoints.
  • Put pagination rules in the contract early: cursor pagination scales better than deep page=500 offsets.

Was this lesson helpful?

Sign in to rate