Field Manual PHP / Laravel reference
← Laravel

Controllers

Controllers coordinate HTTP input, application actions, and responses.

What it is

Controllers coordinate HTTP input, application actions, and responses.

When to use it

Keep HTTP concerns at the boundary and business rules testable.

Syntax

public function show(Order $order): JsonResponse {}

Example

namespace App\Http\Controllers;
use App\Models\Order;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Gate;
class OrderController extends Controller
{
    public function show(Order $order): JsonResponse
    {
        Gate::authorize("view", $order);
        return response()->json(["id" => $order->id, "status" => $order->status]);
    }
}

Common mistakes

Avoid all business logic in one controller; return deliberate fields, not entire sensitive models.

Related topics

Browse Laravel