You’re reading the v0.1 development preview. APIs may change.
~ 1 min readUpdated 2026-10-29

Build a JSON API

Move your response into a controller and add a constrained identifier.

Generate the controller

php elang make:controller PostController

Edit the controller:

namespace App\Controllers;
use Elang\Http\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
final class PostController
{
    public function show(int $id): JsonResponse
    {
        return Response::json(['data' => ['id' => $id, 'title' => 'A small beginning']]);
    }
}

Register a route

use App\Controllers\PostController;
$router->get('/api/posts/{id}', [PostController::class, 'show'], 'posts.show')
    ->where('id', '[0-9]+');

Verify the result

Open /api/posts/42. The JSON should contain an integer ID of 42. /api/posts/nope should return 404 because it fails the route requirement.

Next step

Hardcoded data is useful for understanding the request lifecycle. The database lesson replaces that data with records from SQLite.

Need a hand?

Check the troubleshooting guide, look up a term in the glossary, or work through the complete task board project.

Search Elang documentation

Start typing to find your next step.