Browse Source

add controller in theme support

master
filesite 3 years ago
parent
commit
c24c76f8d7
  1. 2
      controller/Controller.php
  2. 21
      lib/FSC.php

2
controller/Controller.php

@ -47,7 +47,7 @@ Class Controller {
ob_end_flush(); ob_end_flush();
}else { }else {
throw Exception("Layout file not exist: {$layoutFile}", 500); throw new Exception("Layout file not exist: {$layoutFile}", 500);
} }
} }

21
lib/FSC.php

@ -24,7 +24,10 @@ Class FSC {
$layoutName = !empty($config['error_layout']) ? $config['error_layout'] : 'error'; $layoutName = !empty($config['error_layout']) ? $config['error_layout'] : 'error';
$error_layout = __DIR__ . "/../views/layout/{$layoutName}.php"; $error_layout = __DIR__ . "/../views/layout/{$layoutName}.php";
if (!empty($config['theme'])) { if (!empty($config['theme'])) {
$error_layout = __DIR__ . "/../themes/{$config['theme']}/views/layout/{$layoutName}.php"; $theme_error_layout = __DIR__ . "/../themes/{$config['theme']}/views/layout/{$layoutName}.php";
if (file_exists($theme_error_layout)) {
$error_layout = $theme_error_layout;
}
} }
ob_start(); ob_start();
@ -63,6 +66,7 @@ Class FSC {
return compact('controller', 'action'); return compact('controller', 'action');
} }
//add themes support
protected static function loadController($config) { protected static function loadController($config) {
//parse url to controller and action //parse url to controller and action
$requestUrl = $_SERVER['REQUEST_URI']; $requestUrl = $_SERVER['REQUEST_URI'];
@ -78,16 +82,27 @@ Class FSC {
$className = ucfirst($controller) . 'Controller'; $className = ucfirst($controller) . 'Controller';
$funName = 'action' . ucfirst($action); $funName = 'action' . ucfirst($action);
$controllerFile = __DIR__ . "/../controller/{$className}.php"; $controllerFile = __DIR__ . "/../controller/{$className}.php";
if (!empty($config['theme'])) {
$controllerFile = __DIR__ . "/../themes/{$config['theme']}/controller/{$className}.php";
}
if (file_exists($controllerFile)) { if (file_exists($controllerFile)) {
require_once $controllerFile; require_once $controllerFile;
$cls = new $className(); $cls = new $className();
if (method_exists($className, $funName)) { if (method_exists($className, $funName)) {
$cls->$funName(); $cls->$funName();
}else { }else {
throw new Exception("Function {$funName} not exist in class {$className}.", 500); $error_message = "Function {$funName} not exist in class {$className}.";
if (!empty($config['theme'])) {
$error_message = "Function {$funName} not exist in class {$className} of theme {$config['theme']}.";
}
throw new Exception($error_message, 500);
} }
}else { }else {
throw new Exception("Controller {$className} not exist.", 500); $error_message = "Controller {$className} not exist.";
if (!empty($config['theme'])) {
$error_message = "Controller {$className} not exist in theme {$config['theme']}.";
}
throw new Exception($error_message, 500);
} }
} }

Loading…
Cancel
Save