// AJAX form handler
public function savePost(): void
{
$v = Validator::run($_POST)
->set('title', 'Title')->required()->max_len(120)
->set('body', 'Body')->required();
if (!$v->isValid()) {
Response::error('Validation failed', data: $v->getErrors());
return;
}
Database::Go()->insert('posts', [
'title' => Validator::sanitize($_POST['title'], SanitizeType::STRING),
'body' => Validator::sanitize($_POST['body'], SanitizeType::TEXT),
])->run('insert');
Response::success('Post saved.', ['id' => Database::Go()->getLastInsertId()]);
}
// Delete endpoint — nothing to return
public function deletePost(int $id): void
{
Database::Go()->delete('posts')->where('id', $id)->run('delete');
Response::noContent();
}
// Not found
Response::error('Post not found', statusCode: 404);
Framework Docs
Response
Response
The Response class provides a clean API for sending JSON responses, HTTP redirects, raw HTML, and status codes. It is most commonly used in AJAX handlers and API endpoints.
Namespace: Wojo\Core\Response
Response Methods
| Method | Signature | Description |
|---|---|---|
| json() | static json(mixed $data, int $statusCode = 200): void |
Send raw JSON with a given status code |
| success() | static success(string $message, mixed $data = null): void |
Send {"status":"success","message":"...","data":...} |
| alert() | static alert(string $message, mixed $data = null): void |
Send an alert-level response (e.g., warnings) |
| info() | static info(string $message, mixed $data = null): void |
Send an info-level response |
| error() | static error(string $message, int $statusCode = 400, mixed $data = null): void |
Send an error response with HTTP status code |
| html() | static html(string $content, int $statusCode = 200): void |
Send raw HTML with a Content-Type header |
| redirect() | static redirect(string $url, int $statusCode = 302): void |
Send a Location redirect header |
| noContent() | static noContent(): void |
Send HTTP 204 No Content |
| setHeader() | static setHeader(string $name, string $value): void |
Append an arbitrary response header |
| setStatusCode() | static setStatusCode(int $code): void |
Set the HTTP response status code |