Skip to main content

Creating Routes Using Closures

Note: For complete setup instructions including HttpRequestHandler configuration, see Setup.

Closures provide a quick and simple way to define routes inline without creating separate classes. They are ideal for prototyping, testing, or simple endpoints that don't require complex logic.

Basic Example

<?php
use ByJG\RestServer\Route\RouteList;
use ByJG\RestServer\Route\Route;
use ByJG\RestServer\HttpResponse;
use ByJG\RestServer\HttpRequest;
use ByJG\RestServer\OutputProcessor\JsonOutputProcessor;

$routeDefinition = new RouteList();
$routeDefinition->addRoute(
Route::get("/api/test")
->withOutputProcessor(JsonOutputProcessor::class)
->withClosure(function (HttpResponse $response, HttpRequest $request) {
$response->write(['message' => 'Success']);
})
);

When to Use Closures

Best for:

  • Simple routes with minimal logic
  • Prototyping and quick testing
  • Routes that don't need to be reused elsewhere

Consider alternatives for:

Route Parameters

Access route parameters in closures:

<?php
$routeDefinition->addRoute(
Route::get("/api/user/{id}")
->withClosure(function (HttpResponse $response, HttpRequest $request) {
$userId = $request->param('id');
$response->write(['user_id' => $userId]);
})
);

Additional Configuration

For information on: