如何包括页眉/页脚/等php与内容之间的每一页


How to include header/footer/etc.php with content in between for each page?

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

例如,系统是用来管理项目的。所以每个页面都会有页眉/页脚/菜单。例如,两个页面将是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?项目'和'/index.php?notes'.

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

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

TLDR;-我不想把头/脚/等PHP作为一个'包括X'到每个PHP页面模板文件手动。我想让它知道,每一页都需要它,除非另有规定。

创建一个名为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

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

但是如果你坚持,找你的php.ini文件,找到auto_prepend_filehttp://php.net/manual/en/ini.core.php ini.auto-prepend-file

auto_prepend_file header.php // For Example

现在,如果您没有从每个程序中删除header.php(或确保使用include_once),那么您可能会有php错误飞来飞去。