tags:

views:

252

answers:

2

Hi,

I am developing a simple website. It will have close to 20 pages. It has a 3 level hierarchy.

Home
MenuStub
   Category1
      Page1
      Page2
   Category2
      Page1
      Page2
   ....
      ....
      ....

The main navigation will have 4 - 5 items representing each 'category' This will be constant for all the pages. I am not even planning to highlight the current category or anything.

Previously I decided to put the menu HTML stub alone in a separate file and use PHP include to include it in all pages.

But, relative paths might be frustrating. Assume that the menu stub file is located at the root directory.

So, in the root-level pages, the php include will read like

include "menustub.html";

in the second level pages, it should say

include "../menustub.html";

and in third level pages, it should say

include "../../menustub.html";

First, is this the best way to include a single file across all pages in a website?

Second, if the website grows big, and many more levels are added, maintaining this will be a problem. If I suddenly decide to move an entire set of pages one (or several) levels up or down, I should manually go and change the relative paths in each file.

Am I missing something here? Is there a universal way to point to a particular file, that every page will understand, regardless of where it is located?

What is the best way to have a stub and include it in all the pages without having these maintenance nightmares?

+2  A: 

Common ways to solve this problem is either by using include_path: In your config, add the dir with such files to include path, and you can simply do

include "menustub.html";

from anywhere. See http://se2.php.net/manual/en/ini.core.php#ini.include-path It can be set from php.ini and in code. Not sure if it can be set in .htaccess files

Another way is to set a root directory variable and always use it:

include "{$rootDir}/menustub.html";

There is also the option of using auto append/prepend, which means you tell php to always append or prepend a file, see http://se2.php.net/manual/en/ini.core.php (auto_prepend_file)

$rootDir can either be set from configuration, or automatically using $_SERVER['DOCUMENT_ROOT']. The former is good if you use it for shared files (shared between several web apps), the latter for convenience if the files are located under one webapp directory strucure.

Brimstedt
You can also use the `$_SERVER['DOCUMENT_ROOT']` element to find the root directory of your HTTP server, which you can use instead of `{$rootDir}` in your second example. E.g. `include($_SERVER['DOCUMENT_ROOT'] . '/menustub.html');`.
Atli
Hi BrimstedtI want to know more about the include_path. Sounds like the solution I am after. Is it some global thing that all php code (within my website) will know (no matter where the code is). Like I define include_path and put a file - test.html - there, and I can simply use include "test.html" regardless of in which file I am including. Did I get you right?
Senthil
atli: good comment, i added it into the answersenthil: added doc link
Brimstedt
hi. Thanks for editing in, the link for php include path. I'll go through it :)
Senthil
Atli. I can use DOCUMENT_ROOT, but what about the difference between debug mode and deployment? In the production server, a file might be directly under DOCUMENT_ROOT, but during debugging, in XAMPP, which I use, I will have a separate folder for my website. so it will be DOCUMENT_ROOT+some folder+file name. I have to remember to remove that extra folder every time I deploy.
Senthil
You could just create a config php file, where you define a constant that represents the root directory of your project. Then you include that config file into your scripts and use the constant instead of DOCUMENT_ROOT. That way you just change the constant in the config file once and never have to worry about it again.
Atli
Hmm.. I tried something like what you are saying. But I never really arrived at a proper, hassle-free procedure to painlessly deploy with all the paths working etc... Maybe because I missed out something small. Anyway, thanks for explaining how to deal with includes :)
Senthil
+2  A: 

Hey.

It would be much more dynamic to have the index file do all the including, and use GET parameters to tell it which page is to be include.

For example, consider a directory structure like this:

/
  index.php
  /ui
    header.html
    menu.html
    footer.html
    Cat1/
      Page1.html
      Page2.html
    Cat2/
      Page3.html
      Page4.html

If you were to always call the index file, including the name of the category and page you wanted to see, like: index.php?category=Cat1&page=Page1, you could do something like this:

<?php
// Include a header and a menu, defined in their own HTML files.
include('ui/header.html');
include('ui/menu.html');

// Set up a list of valid categories and their sub pages.
$pages = array(
    'Cat1' => array(
        'Page1',
        'Page2'
    ),
    'Cat2' => array(
        'Page3',
        'Page4'
    )
);

// Find the category and page to use.
if(isset($_GET['category'], $pages[$_GET['category']])) {
    $category = $_GET['category'];
} else {
    $category = 'Cat1';
}
if(isset($_GET['page'], $pages[$category][$_GET['page']])) {
    $page = $_GET['page'];
} else {
    $page = 'Page1';
}

// Include the selected content page.
include("ui/{$category}/{$page}.html");

// Include a footer
include('ui/footer.html');
?>

This way you can expand the content as far and deep as you want without having to repeat your includes in every single new file.

Atli
Senthil
As you start working on more dynamic stuff - using more sophisticated design methods - this does become the standard method yes. I recommend you try to get used to it sooner rather than later. -- As to the URL question. Look in to Url-Rewriting. If you use Apache, you can accomplish this by using the `mod_rewrite` module. *(See `http://httpd.apache.org/docs/2.0/misc/rewriteguide.html`)*
Atli
hi.. thanks for confirming that this is a recommended/standard approach. Also, thanks for the mod_rewrite module. Will look into it too.
Senthil