From b984586e30e3f6a950a4107596be4e075eb1e5f0 Mon Sep 17 00:00:00 2001 From: filesite Date: Wed, 28 Sep 2022 23:52:51 +0800 Subject: [PATCH] add default controller, layout and view file support --- controller/Controller.php | 25 ++++++++++++++++++------- lib/FSC.php | 17 ++++++++++++++--- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/controller/Controller.php b/controller/Controller.php index e02cc48..f609098 100644 --- a/controller/Controller.php +++ b/controller/Controller.php @@ -24,25 +24,36 @@ Class Controller { //render view protected function render($viewName, $viewData = array(), $pageTitle = '') { - $layoutFile = __DIR__ . '/../views/layout/' . $this->layout . '.php'; - $viewFile = __DIR__ . '/../views/' . FSC::$app['controller'] . '/' . $viewName . '.php'; + $baseLayoutFile = $themeLayoutFile = $layoutFile = ''; + $baseViewFile = $themeViewFile = $viewFile = ''; + + $baseLayoutFile = __DIR__ . '/../views/layout/' . $this->layout . '.php'; + $baseViewFile = __DIR__ . '/../views/' . FSC::$app['controller'] . '/' . $viewName . '.php'; //双斜杠//开头的共享视图支持 if (preg_match('/^\/\//', $viewName)) { - $viewFile = __DIR__ . '/../views/' . str_replace('//', '/', $viewName) . '.php'; + $baseViewFile = __DIR__ . '/../views/' . str_replace('//', '/', $viewName) . '.php'; } if (!empty(FSC::$app['config']['theme'])) { - $layoutFile = __DIR__ . '/../themes/' . FSC::$app['config']['theme'] . '/views/layout/' . $this->layout . '.php'; - $viewFile = __DIR__ . '/../themes/' . FSC::$app['config']['theme'] . '/views/' . FSC::$app['controller'] . '/' . $viewName . '.php'; + $themeLayoutFile = __DIR__ . '/../themes/' . FSC::$app['config']['theme'] . '/views/layout/' . $this->layout . '.php'; + $themeViewFile = __DIR__ . '/../themes/' . FSC::$app['config']['theme'] . '/views/' . FSC::$app['controller'] . '/' . $viewName . '.php'; //双斜杠//开头的共享视图支持 if (preg_match('/^\/\//', $viewName)) { - $viewFile = __DIR__ . '/../themes/' . FSC::$app['config']['theme'] . '/views/' . + $themeViewFile = __DIR__ . '/../themes/' . FSC::$app['config']['theme'] . '/views/' . str_replace('//', '/', $viewName) . '.php'; } } + if (!empty($themeLayoutFile) && file_exists($themeLayoutFile)) { + $layoutFile = $themeLayoutFile; + $viewFile = $themeViewFile; + }else if (file_exists($baseLayoutFile)) { + $layoutFile = $baseLayoutFile; + $viewFile = $baseViewFile; + } + //include layout and view - if (file_exists($layoutFile)) { + if (!empty($layoutFile)) { ob_start(); include_once $layoutFile; diff --git a/lib/FSC.php b/lib/FSC.php index 294b290..226f635 100644 --- a/lib/FSC.php +++ b/lib/FSC.php @@ -81,11 +81,22 @@ Class FSC { //call class and function $className = ucfirst($controller) . 'Controller'; $funName = 'action' . ucfirst($action); - $controllerFile = __DIR__ . "/../controller/{$className}.php"; + + $controllerFile = $baseControllerFile = $themeControllerFile = ''; + + $baseControllerFile = __DIR__ . "/../controller/{$className}.php"; if (!empty($config['theme'])) { - $controllerFile = __DIR__ . "/../themes/{$config['theme']}/controller/{$className}.php"; + $themeControllerFile = __DIR__ . "/../themes/{$config['theme']}/controller/{$className}.php"; } - if (file_exists($controllerFile)) { + + //优先使用皮肤目录下的控制器,其次默认controller目录下的 + if (!empty($themeControllerFile) && file_exists($themeControllerFile)) { + $controllerFile = $themeControllerFile; + }else if (file_exists($baseControllerFile)) { + $controllerFile = $baseControllerFile; + } + + if (!empty($controllerFile)) { require_once $controllerFile; $cls = new $className(); if (method_exists($className, $funName)) {