如何在每个页面之间包含 header/footer/etc.php 内容?
How to include header/footer/etc.php with content in between for each page?
我正在寻找一个面向对象的解决方案,或者可能是一个已经存在的框架来完成我希望为一个较小的 back-end/admin 项目做的事情。
比如系统是用来管理Projects的。所以每一页都会有一个header/footer/menu。例如,两个页面将是 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
并且让我的每个页面的 links 只是“/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 -->
并让我菜单中的 links 为“/index.php?projects”和“/index.php?notes”。
两者都令人沮丧,而且对我来说似乎不太流畅。我想在这里升级我的策略,但不确定最好的方法。
最好的方法是什么?是否有一个轻量级的框架可以为我管理这些?我想将 links 保留在 '/projects/' 和 '/notes/' 处,但只能创建一个对象来调用 projects.php 中的内容并将其自动放置在 link点击。
TLDR; - 我不想手动将 header/footer/etc.php 作为 'include X' 放入每个 PHP 页面模板文件中。我想让它知道每个页面都需要它,除非另有指定。
每个文件?你真的确定要那样做吗?这可能会导致数据崩溃。
但如果您坚持,请查找您的 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 错误。
创建一个名为 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=notes
或 page.php?page=projects
我正在寻找一个面向对象的解决方案,或者可能是一个已经存在的框架来完成我希望为一个较小的 back-end/admin 项目做的事情。
比如系统是用来管理Projects的。所以每一页都会有一个header/footer/menu。例如,两个页面将是 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
并且让我的每个页面的 links 只是“/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 -->
并让我菜单中的 links 为“/index.php?projects”和“/index.php?notes”。
两者都令人沮丧,而且对我来说似乎不太流畅。我想在这里升级我的策略,但不确定最好的方法。
最好的方法是什么?是否有一个轻量级的框架可以为我管理这些?我想将 links 保留在 '/projects/' 和 '/notes/' 处,但只能创建一个对象来调用 projects.php 中的内容并将其自动放置在 link点击。
TLDR; - 我不想手动将 header/footer/etc.php 作为 'include X' 放入每个 PHP 页面模板文件中。我想让它知道每个页面都需要它,除非另有指定。
每个文件?你真的确定要那样做吗?这可能会导致数据崩溃。
但如果您坚持,请查找您的 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 错误。
创建一个名为 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=notes
或 page.php?page=projects