API App
REST API with controllers, health route, and JSON request handling.
Setup
bash
composer create-project nexphant/nexphant my-app --template api
cd my-appController
php
<?php
namespace App\Controllers;
use Nexphant\Route;
use Nexphant\Get;
use Nexphant\Post;
use Nexphant\Request;
#[Route('/api')]
class EchoController
{
#[Get('/health')]
public function health(): array
{
return ['status' => 'ok', 'time' => time()];
}
#[Post('/echo')]
public function echo(Request $request): array
{
$body = $request->json();
return ['echo' => $body];
}
}app.php
php
<?php
require __DIR__ . '/vendor/autoload.php';
use Nexphant\App;
$app = App::create();
$app->annotations(base_path('app/Controllers'));
$app->listen(port: 8080);Test
bash
# health
curl http://localhost:8080/api/health
# echo
curl -X POST http://localhost:8080/api/echo \
-H 'Content-Type: application/json' \
-d '{"message":"hello"}'JSON Request
Request::json() parses the request body as JSON:
php
$body = $request->json();
$name = $request->json('name');
$page = $request->json('page', 1);