我使用以下作为 Frontpage/Page Controller(s),到目前为止它工作正常,除了我面临的两个问题,如你所见,$pages 数组和开关实际上比我的那个长得多'已经粘贴在这里。每次需要新的页面控制器时,我都必须将其添加到 $pages 数组并进行切换,这会使该列表非常长。您将如何克服这个问题,您是否看到此代码有任何其他改进?loadLogic()
在页面控制器中,它用于获取 pages/controllername/logic/function.php 下的函数。
Frontpage 控制器 - index.php:
include 'common/common.php';
if(!isset($_GET['p']) OR $_GET['p'] == ''){
$_GET['p'] = 'home';
header('Location: index.php?p=home');
}
$pages = array('home','register','login','logout','page1','page2','page3');
$_GET['p'] = trim($_GET['p']);
if(isset($_GET['p'])){
if(in_array($_GET['p'], $pages)){
switch ($_GET['p']) {
case 'home':
include 'home.php';
break;
case 'register':
include 'register.php';
break;
case 'login':
include 'login.php';
break;
case 'logout':
include 'logout.php';
break;
case 'page1':
include 'page1.php';
break;
case 'page2':
include 'page2.php';
break;
case 'page3':
include 'page3.php';
break;
}
}else{
echo '404!';
}
}
页面控制器 - {home,register,login,logout,page1,page2,page3}.php:
include 'tpl/common/header.php';
contentStart();
if(isset($_SESSION['logged'])){
loadLogic('dashboard');
}else{
loadLogic('nologin');
}
//Display login form in logic page instead links
//
if(!isset($_SESSION['logged'])){
contentEnd();
loadLogic('nologinForm');
}else{
contentEnd();
include'tpl/common/rcol.php';
}
include 'tpl/common/footer.php';
函数加载逻辑():
function loadLogic($logic) {
$path = dirname(__DIR__) . '/pages';
$controller = preg_split('/&/',$_SERVER['QUERY_STRING']);
$controller = trim($controller[0],"p=");
$logicPath = 'logic';
$logic = $logic . '.php';
$err = 0;
$logicFullPath = $path.'/'.$controller.'/'.$logicPath.'/'.$logic;
if($err == '0'){
include "$logicFullPath";
}
}
文件夹结构:
projectName
|
---> common
|
---> pages
| |
| --->home
| |
| --->register
| |
| --->login
| |
| --->logout
| |
| --->page1
| |
| --->page2
| |
| --->page3
|
---> tpl
| |
| ---> common
|
--> home.php
|
--> register.php
|
--> login.php
|
--> logout.php
|
--> page1.php
|
--> page2.php
|
--> page3.php