WizdomWeb/app/Core/Controller.php

71 lines
2.3 KiB
PHP

<?php
namespace WizdomNetworks\WizeWeb\Core;
use WizdomNetworks\WizeWeb\Utils\Logger;
abstract class Controller
{
/**
* Render a view file with the given data.
*
* @param string $view The path to the view file.
* @param array $data Data to be extracted and used in the view.
* @throws \Exception If the view file cannot be found.
*/
protected function render(string $view, array $data = []): void
{
try {
// Extract data for use in the view
extract($data);
// Resolve the view path
$viewPath = __DIR__ . "/../../resources/views/$view.php";
Logger::info("[DEBUG] Resolving view: $view | Path: $viewPath");
if (file_exists($viewPath)) {
Logger::info("[DEBUG] Including view file: $viewPath");
include $viewPath;
Logger::info("[DEBUG] Successfully rendered view: $view");
} else {
throw new \Exception("View file not found: $view\nPath: $viewPath");
}
} catch (\Exception $e) {
// Log the exception with Logger
Logger::error("[ERROR] Exception while rendering view: " . $e->getMessage());
throw $e;
}
}
/**
* Get the rendered content of the contact form.
*
* This method renders the contact form view and returns the output as a string.
* It is used for embedding the contact form in various parts of the application.
*
* @param array $data Optional data to pass to the contact form view.
* @return string The rendered contact form HTML.
* @throws \Exception If the contact form view cannot be rendered.
*/
protected function getContactForm(array $data = []): string
{
try {
Logger::info("[DEBUG] Rendering contact form with data: " . json_encode($data));
// Buffer output to capture rendered content
ob_start();
$this->render('partials/contact_form', $data);
$output = ob_get_clean();
Logger::info("[DEBUG] Successfully rendered contact form");
return $output;
} catch (\Exception $e) {
// Log any exception that occurs
Logger::error("[ERROR] Exception while rendering contact form: " . $e->getMessage());
throw $e;
}
}
}