Response
The Io library also handles HTTP responses — setting status codes, headers, content,
and sending JSON or HTML back to the client.
It is available in controllers via $this->io.
Overview
A typical response is built in three steps:
Set a status code with
set_status_code()Set headers and content
Call
send()to flush everything to the client
<?php
$this->response->set_status_code(200);
$this->response->set_html_content('<h1>Hello World</h1>');
$this->response->send();
Setting a Status Code
<?php
$this->response->set_status_code(200); // OK
$this->response->set_status_code(201); // Created
$this->response->set_status_code(400); // Bad Request
$this->response->set_status_code(401); // Unauthorized
$this->response->set_status_code(403); // Forbidden
$this->response->set_status_code(404); // Not Found
$this->response->set_status_code(500); // Internal Server Error
Adding Headers
Single header:
<?php
$this->response->add_header('Cache-Control', 'no-cache');
Multiple headers at once (pass an array):
<?php
$this->response->add_header([
'Cache-Control' => 'no-store',
'X-Content-Type-Options' => 'nosniff',
'X-Frame-Options' => 'DENY',
]);
Note
Headers are queued internally and only sent when send() is called.
Setting Content
Plain or HTML content:
<?php
$this->response->set_content('<p>Hello</p>');
HTML content (automatically adds ``Content-Type: text/html``):
<?php
$this->response->set_html_content('<h1>Welcome</h1>');
Sending the Response
send() flushes the status code, all queued headers, and the content body:
<?php
$this->response->set_status_code(200);
$this->response->add_header('X-Custom-Header', 'value');
$this->response->set_html_content('<p>Response body</p>');
$this->response->send();
Sending a JSON Response
send_json() automatically sets Content-Type: application/json, encodes the data,
and calls send():
<?php
$this->response->set_status_code(200);
$this->response->send_json([
'status' => 'success',
'message' => 'User created',
'id' => 42,
]);
JSON error response:
<?php
$this->response->set_status_code(422);
$this->response->send_json([
'status' => 'error',
'message' => 'Validation failed',
'errors' => [
'email' => 'The email field is required.',
],
]);
Common Response Patterns
200 OK with JSON data:
<?php
public function show($id)
{
$this->call->model('UserModel');
$user = $this->UserModel->find($id);
if (!$user) {
$this->response->set_status_code(404);
$this->response->send_json(['message' => 'User not found']);
}
$this->response->set_status_code(200);
$this->response->send_json($user);
}
201 Created after insert:
<?php
public function store()
{
$this->call->model('UserModel');
$id = $this->UserModel->insert($this->response->post());
$this->response->set_status_code(201);
$this->response->send_json(['message' => 'Created', 'id' => $id]);
}
204 No Content after delete:
<?php
public function destroy($id)
{
$this->call->model('UserModel');
$this->UserModel->delete($id);
$this->response->set_status_code(204);
$this->response->send();
}
HTML response with custom headers:
<?php
public function page()
{
$this->response->set_status_code(200);
$this->response->add_header('X-Frame-Options', 'SAMEORIGIN');
$this->response->set_html_content('<h1>Hello from LavaLust</h1>');
$this->response->send();
}
Redirect using a header:
<?php
public function old_page()
{
$this->response->set_status_code(301);
$this->response->add_header('Location', '/new-page');
$this->response->send();
}
Method Reference
Method |
Returns |
Description |
|---|---|---|
|
void |
Sets the HTTP response status code |
|
void |
Queues one or more response headers |
|
void |
Sets the raw response body |
|
void |
Sets the response body and adds |
|
void |
Sends the status code, all queued headers, and the content body |
|
void |
Encodes |