1

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.

5
  • Why not use $_GET['page'] which is equal to projects or notes. Based on the get your can load (include) the right php file Commented Aug 31, 2016 at 20:53
  • That's just the inverse of my second solution, right? Seems like the same annoyance there would exist it would just be written differently. Commented Aug 31, 2016 at 20:57
  • No beceause you don't have to write an if statement for evey file. Assuming that every header, footer, menu file consist of the same content Commented Aug 31, 2016 at 20:58
  • Did any of the answers help you? If not or if you still have problems update the question and let us know Commented Sep 1, 2016 at 17:32
  • Yea, definitely helped. I guess what I was looking for isn't feasible or I wasn't explaining it properly. I was hoping there was a better way. i.e., something like just having $projects = new PageTemplate(); and then have a list of the page objects that would be called and loaded when the subdirector was called.. like have the url /projects/. I didn't want to have any ?= variables in the url unless they were actually editing.. so like projects/?edit=1?project_id=12 instead of page.php?page=projects&edit=1&project_id=12. Commented Sep 2, 2016 at 14:34

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Well, not physically every file; as I also have files like "functions.php" that contain real functions that don't display anything. But, I would like to add header.php to the top of every "page of type X", for instance.
This only applies to the file being called, not every file that exists so if you include functions.php you don't get a 2nd copy of header.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.