/** * Dispatch the request. */ public function dispatch() { if ($this->active) { // Initialize the core model $coreModel = new CoreModel($this->request); // Validate the request $requestValid = $coreModel->validateRequest(); $output = ''; if ($requestValid) { // Retrieve the correct module controller $controllerObj = $this->getRequestController(); // In case the controller could not be initialized, throw an exception if (!$controllerObj) { ErrorHandler::error(E_ERROR, 'The requested endpoint could not be initialized'); } // In case the module is inactive or the requested method does not exist, throw an exception if (!$controllerObj->active || !method_exists($controllerObj, $this->action)) { ErrorHandler::error(E_ERROR, "The requested action '%s' is not available", $this->action); } // Start an output buffer to catch request content ob_start(); // Execute the before action when present $beforeMethodName = 'before' . ucfirst($this->action); if (method_exists($controllerObj, $beforeMethodName)) { $controllerObj->{$beforeMethodName}(); } // Execute the requested action $controllerObj->{$this->action}(); // Execute the after action when present $afterMethodName = 'after' . ucfirst($this->action); if (method_exists($controllerObj, $afterMethodName)) { $controllerObj->{$afterMethodName}(); } // Retrieve the output buffer result $result = ob_get_clean(); // In case the request is AJAX, output the request result directly if ($this->request->ajax) { // Retrieve the header include content $header = $this->getHeaderIncludeHTML(); $output = $header . $result; } else { // Retrieve the output ob_start(); require_once $this->modulePath . DIR_VIEW . 'index.php'; $output = ob_get_clean(); } } } else { $output = $this->getMaintenanceView(); } // Set the output character set header('Content-type: text/html; charset=utf-8'); // header('Cache-Control: max-age=3600'); // Send the output exit($output); }