我已经上传了一个示例网站的zip文件到http://superuntitled.com/default/default.zip如果你喜欢,请看看。
我喜欢我所有的HTML内容的存储包含文件,并留下我的index.php文件将这些文件调入一个template.php文件中...
require_once('common.php');
$view = (isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : '';
switch ($view) {
case '' :
$view = 'home';
$section = 'templates/template-default.php';
$pageTitle = "The Homepage";
break;
case 'about' :
$view = 'about';
$section = 'templates/template-about.php';
$pageTitle = "The About Page";
$sidebar = 1;
break;
case 'notfound' :
$section = "templates/template-not-found.php";
$pageTitle = "404 not found";
break;
}
require_once 'view/template.php';
模板文件一样简单因为这样:
require_once('header.php');
if(isset($sidebar))include("view/sidebar.php");
echo "<div id='content'>";
require_once (SERVER_URL.$section);
echo "</div>";
require_once('footer.php');
这样我就可以在index.php文件中设置各种变量。使用htaccess“漂亮的网址”很简单(RewriteRule ^([-\A-Za-z0-9\_]*)$ index.php?page=$1
)。
common.php文件包含所有全局变量,会话变量并包含数据库连接文件和php函数文件。
至于活动课,$视图变量可以对在导航部分进行检查,我用一个简单的PHP函数:
function curPage($current, $pageTitle) {
if ($pageTitle==$current)
echo " class=\"selected\"";
}
而在导航部分,我只需调用这个函数:
<a href="about" <? curPage('about',"$view"); ?> >About</a>
这种方式保持干净和简单。
我确实记得在htaccess文件中使用mod_rewrite重写URL之前的一次。谢谢你提醒我。 您的建议将页眉和页脚文件粘贴到/ inc文件夹听起来不错。我通常会将这些文件留在根目录中,因为我似乎总是陷入困境。我会看看我能否做到这一点,以免我再次遇到这个问题。 – Justin 2010-07-03 03:50:59
数据库连接文件也应该保存在文档根目录 – RSK 2010-07-03 04:13:36