Routing
Basic Routes
php
use Nexphant\App;
$app = App::create();
$app->get('/', fn () => ['message' => 'Home']);
$app->post('/users', fn () => ['created' => true]);
$app->put('/users/{id}', fn ($req, $res, $params) => ['updated' => $params['id']]);
$app->delete('/users/{id}', fn ($req, $res, $params) => ['deleted' => $params['id']]);Route Parameters
php
$app->get('/users/{id}', function ($request, $response, $params) {
return ['user_id' => $params['id']];
});Request Injection
php
use Nexphant\Request;
$app->get('/search', function (Request $request) {
$query = $request->query('q');
return ['query' => $query];
});Groups
php
$app->group('/api', function ($app) {
$app->get('/users', fn () => ['users' => []]);
$app->get('/posts', fn () => ['posts' => []]);
});Fast Routes
Fast routes are fixed-path responses registered directly in the hot path. Use them for health checks, readiness probes, static metadata, robots files, and other endpoints that do not need route parameters or middleware-heavy request handling.
php
$app->fastJson('GET', '/health', ['status' => 'ok']);
$app->fastJson('GET', '/version', [
'name' => 'my-app',
'version' => '1.0.0',
]);
$app->fastText('GET', '/robots.txt', "User-agent: *\nAllow: /\n");
$app->fastRaw('GET', '/ping.raw', "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\npong");Fast routes must use concrete paths. Avoid placeholders such as /users/{id} because dynamic routes need the normal router.
When working directly with Nexphant\Server\HttpServer, the same pattern is available:
php
$server->fastJson('GET', '/health', ['status' => 'ok']);
$server->fastText('GET', '/robots.txt', "User-agent: *\nAllow: /\n");
$server->fastCachedJson('GET', '/stats', fn () => ['uptime' => time()], ttl: 1);Attributes
php
use Nexphant\Get;
use Nexphant\Post;
class UserController
{
#[Get('/users')]
public function index()
{
return ['users' => []];
}
#[Post('/users')]
public function store(Request $request)
{
return ['created' => true];
}
}
$app->annotations(UserController::class);