Response Class
The Response Class handles application output and termination. It provides methods for rendering theme pages, sending JSON responses, CSV exports, and properly closing database connections and saving settings before terminating execution.
render($view, $response = [], $theme = 'default')
The recommended method for modern applications. Automatically handles both HTML and JSON responses based on the request type. This method detects whether the client expects JSON (via Accept header or page-output parameter) and responds accordingly.
$view(string): The view or HTML content to render.$response(array): An associative array containing response data such as 'html', 'success', 'msg', etc. If is JSON request, it will be automatically converted to JSON format.$theme(string): The theme to use for rendering the page 'default'|'public'|'empty' ... .
// Modern pattern with TableBuilder and TitleBuilder
$response = (new TableBuilder($model, 'table_id'))
->setDefaultActions()
->getResponse();
$html = (new TitleBuilder('Page Title'))
->addButton('Add New', '?page=example&action=edit', 'primary')
->addSearch('search_id', 'Search...', 'Search')
->render() . '
' . $response['html'];
// Automatically handles JSON for AJAX requests or HTML for regular requests
Response::render($html, $response);
// Custom response data
Response::render('Custom content', [
'success' => true,
'msg' => 'Operation completed successfully',
'additional_data' => ['key' => 'value']
]);
Required Response Parameters
The $response array should contain these key parameters:
- html (string): The HTML content to display
- success (boolean): Indicates if the operation was successful
- msg (string): Message to display to the user
If these parameters are not provided, the system will auto-generate them from MessagesHandler.
Automatic JSON Detection
The render method automatically detects JSON requests through:
- Accept Header: Checks for 'application/json' in HTTP_ACCEPT
- Request Parameter: Looks for
$_REQUEST['page-output'] = 'json'
Response Examples
// HTML Response (regular page request)
// URL: ?page=posts
$response = ['html' => '...
', 'success' => true, 'msg' => 'Data loaded'];
Response::render($response['html'], $response);
// Output: Full HTML page with theme
// JSON Response (AJAX request)
// URL: ?page=posts&page-output=json
// Or with Accept: application/json header
$response = ['html' => '...
', 'success' => true, 'msg' => 'Data loaded'];
Response::render($response['html'], $response);
// Output: {"html":"...
","success":true,"msg":"Data loaded"}
// Practical AJAX example with TableBuilder
$response = (new TableBuilder($model, 'posts_table'))
->setDefaultActions()
->getResponse();
$html = (new TitleBuilder('Posts'))
->render() . '
' . $response['html'];
// For regular requests: shows full page
// For AJAX requests: returns JSON with updated HTML
Response::render($html, $response);
denyAccess()
Terminates the application and redirects to the deny page or responds with JSON. This method checks if the request expects a JSON response and handles it accordingly.
// Deny access example
if (!Permissions::check('auth.manage')) {
Response::denyAccess();
}
themePage($page, $content, $variables)
Legacy Theme Management
Loads a theme page with the specified content and variables. This method loads the requested theme page, passes the required variables to it, and terminates the application after rendering. The page name is mandatory and generally prints the content passed in Theme::set('content').
// Three possible ways to call theme_page
// 1. pass only the theme page name (will load page_name.page.php file)
Theme::set('content', '...');
Response::themePage('page_name');
// 2. pass the page name and content
Response::themePage('page_name', '', 'content');
// 3. pass the page name, template to load and variables for the template
Response::themePage('theme_page', __DIR__ . '/assets/Modules_page.php', ['my_vars' => '...']);
json(array $data)
Responds with JSON data and terminates the application. This method sends a JSON response to the client, saves settings, closes database connections, and ends execution.
Response::json(['status' => 'success', 'data' => $result]);
csv(array $data, string $filename = 'export')
Responds with CSV data and terminates the application. This method sends a CSV response to the client for file download, properly handles DateTime objects and nested data structures, saves settings, closes database connections, and ends execution.
// Export data as CSV
$data = [
['name' => 'John', 'email' => 'john@example.com', 'created_at' => new DateTime()],
['name' => 'Jane', 'email' => 'jane@example.com', 'created_at' => new DateTime()]
];
Response::csv($data, 'users_export');
Method Reference
| Method | Description | Use Case |
|---|---|---|
render($view, $response, $theme) |
Modern method for automatic JSON/HTML handling | Recommended for all new applications |
themePage($page, $content, $variables) |
Legacy method for theme rendering | Direct theme control, legacy compatibility |
json($data) |
Direct JSON response | API endpoints, AJAX responses |
htmlJson($response) |
JSON with HTML content and standardized format | Internal use by render() method |
csv($data, $filename) |
CSV file download | Data export functionality |
isJson() |
Check if request expects JSON | Internal method for automatic detection |
Important Notes
Application Termination: All Response methods terminate the application execution using exit after completing their tasks. This ensures proper cleanup by:
- Saving system settings with
Settings::save() - Closing primary database connection with
Get::closeConnections(); - Closing secondary database connection with
Get::db2()->close() - Running end-page hooks (for theme_page method)
Security: The theme_page method includes path sanitization to prevent directory traversal attacks and supports theme customizations through the customizations directory.