如何在每个页面之间包含页眉/页脚/ etcphp,其中包含内容?

时间:2016-08-31 20:45:31

标签: php templates frameworks include backend

我正在寻找一个面向对象的解决方案,或者可能是一个已经存在的框架来做我希望为一个有点小的后端/管理项目做的事情。

例如,系统是管理项目。因此每页都会有一个页眉/页脚/菜单。例如,两个页面是projects.php和notes.php。

要在这些页面上包含页眉和页脚,我通常会这样做:

//header.php
<!-- Header Content Here -->
include menu.php

//projects.php
include header.php
<!-- My Projects Content Here -->
include footer.php

//notes.php
include header.php
<!-- My Projects Content Here -->
include footer.php

并将每个页面的链接设为'/ projects /'或'/ notes /'

我也试过了:

//index.php
include header.php
include projects.php
include notes.php
include footer.php

//projects.php
if(isset($_GET['projects']) {
<!-- My Projects Content Here -->

//notes.php
if(isset($_GET['notes']) {
<!-- My Notes Content Here -->

并且我的菜单中的链接是'/index.php?projects'和'/index.php?notes'。

两者都令人沮丧,对我来说似乎不太流畅。我想在这里升级我的策略,但我不确定最好的方法。

最好的方法是什么?是否有一个轻量级的框架,可以为我管理这些?我想保留'/ projects /'和'/ notes /'中的链接,但只能创建一个从projects.php调用内容的对象,并将其自动放置在该链接点击上。

TLDR; - 我不想手动将header / footer / etc.php作为'include X'放入每个PHP页面模板文件中。我希望它知道每个页面都需要它,除非另有指定。

2 个答案:

答案 0 :(得分:1)

创建一个名为page.php的文件:

if(!empty($_GET['page'])) {
  // Could add check to see if file exists
  $page = $_GET['page']; // Being "notes" or "projects" 

  include 'header.php'; // Assuming this includes <head>, header, menu, functions etc. 
  include $page.'.php'; // Makes notes.php or projects.php
  include 'footer.php';

} else {
 echo 'Someting went wrong';
}

对于菜单中的链接,它应为page.php?page=notespage.php?page=projects

答案 1 :(得分:0)

每个文件?你真的确定要这么做吗?它可能会导致数据崩溃。

但如果您坚持,请查找您的php.ini文件并查找auto_prepend_file http://php.net/manual/en/ini.core.php#ini.auto-prepend-file

auto_prepend_file header.php // For Example

既然你没有从每个程序中删除header.php(或确保使用include_once),那么你就可以这样做了,那么你可能会遇到php错误。

相关问题