PHP Frontpage/页面控制器
我使用以下内容作为 Frontpage/Page 控制器,到目前为止它工作正常,除了我面临的两个问题,如您所见,$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';
函数 loadLogic():
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
I using the following as Frontpage/Page Controller(s) and it's working ok so far, except two problems I'm facing which as you can see are the $pages array and the switch, which are actually much much longer as the one I've pasted here. Everytime there is a need for a new page controller I have to add it to $pages array and to switch which makes that list very long. How would you overcome this problem and do you see any other improvement on this code? loadLogic()
in page controllers it is used to get functions under pages/controllername/logic/function.php.
Frontpage Controller - 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!';
}
}
Page Controller - {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():
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";
}
}
Folder Structure:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于frontpage控制器,为什么有这么多case语句。您已经知道哪些页面可以有效包含,并检查它是否在有效页面中。
你可以这样做:
如果你希望它有不同的名称传递给 $_GET 进行混淆,以及不同的潜在扩展,那么你可以这样做:
如果你想让页面数组更容易管理,你可以将它分成多个lines:
哦,还有,因为主页 http://www.domain 有点难看.com/?p=home,如果 p 的值不在数组中或不是数组键(具体取决于您使用的值),则只需将 home 设为默认包含即可。
所以:
那么你可以去掉 if !isset $_GET['p'] or $_GET['p'] == '' 在顶部。您还可以使用 && 将 isset 和 in_array/array_key_exists 组合到同一 if 语句中。如果第一次评估是错误的并且它命中了 &&然后它只是停止并且不评估其余部分,所以没有错误或任何东西,这也意味着您可以轻松地设置一次默认响应,因为嵌套它们意味着您必须为两个 if 都有一个默认值。
更多编辑。如果您确实希望在用户尝试访问不存在的 ap= 时出现 404,而不只是引导到主页,您可以在顶部执行此操作:
然后在 If 结构中向下包含页面,在 else include home.php 之前执行 else if ,例如:
这样,如果 p 已设置但未签出,则它将包含 404,但如果未设置,它将转到 home.php
For the frontpage controller, why so many case statements. You already know what pages are valid to include and you check if it's in the valid pages.
You can just do:
If you wanted it to have different names passed to $_GET for obfuscation, along with different potential extensions, then you could do:
If you wanted to make the array of pages more easily managable you can break it up into multiple lines:
Oh, also, since it's sort of ugly to have the home page be http://www.domain.com/?p=home, just make home be the default include if the value for p is not in the array or not an array key, depending on which you'd use.
So:
Then you can get rid of that if !isset $_GET['p'] or $_GET['p'] == '' at the top. You can also combine the isset and in_array/array_key_exists into the same if statement with an &&. If the first evaluation is false and it hits an && then it just stops and doesn't evaluate the rest, so no errors or anything, and it also means that you can easily set a default response just once, as having them nested means that you'd have to have a default for both ifs.
More edits. If you really wanted to have a 404 when a user tries to go to a p= that's non existant instead of just getting booted to the home page, you could do this at the top:
and then down in the If structure to include the pages, do an else if before the else include home.php like:
So that if p is set but doesn't check out then it will include 404, but if it isn't set, it will go to home.php