Picasso-ITS
Picasso-ITS

Reputation: 27

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

I'm looking for an object-oriented solution or possibly a framework that already exists to do what I'm hoping to do for a somewhat small back-end/admin project.

For instance, the system is to manage Projects. So there will be a header/footer/menu on every page. Two pages, for instance, will be projects.php and notes.php.

To include the header and footer on these pages I would normally do:

//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

and have my links to each page just be '/projects/' or '/notes/'

I've also tried:

//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 -->

and have the links in my menu be '/index.php?projects' and '/index.php?notes'.

Both are frustrating and don't seem very fluid to me. I would like to upgrade my tactics here but am unsure the best way to go about it.

What's the best way? Is there a Framework that is lightweight and can manage these for me? I would like to keep the links at '/projects/' and '/notes/' but just be able to create an object that calls the content from projects.php and places it in automatically on that link click.

TLDR; - I don't want to have to place header/footer/etc.php as an 'include X' into every PHP page template file manually. I would like for it to know that every page needs it unless otherwise assigned.

Upvotes: 1

Views: 1000

Answers (2)

SuperDJ
SuperDJ

Reputation: 7661

Create a file called something like 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';
}

For links in menu it should be page.php?page=notes or page.php?page=projects

Upvotes: 1

Forbs
Forbs

Reputation: 1276

Every file? Are you really sure you want to do that? It might cause data crash.

But if you insist, look for your php.ini file and find auto_prepend_file http://php.net/manual/en/ini.core.php#ini.auto-prepend-file

auto_prepend_file header.php // For Example

Now that you did that if you don't remove header.php from every program (or make sure include_once is used) then you might have php errors flying around.

Upvotes: 0

Related Questions