0

I am learning and I got two different opinions and ways of this very trivial step. I have different pages in a project like Home, About, Contacts etc..

I did the following: An index.php files and parameters like ?page=about, ?page=contacts etc. I have a switch statement with $_GET['page'] for every parameter. For every case I include a different file for the page content.

Example:

switch ($current_page) {
  case ('about'):
    include VIEWS_PATH . PAGES_CONTENTS . 'about.php';
    break;
  case ('contacts'):
    include VIEWS_PATH . PAGES_CONTENTS . 'contacts.php';
    break;
  default:
    include VIEWS_PATH . PAGES_CONTENTS . 'homepage.php';
}

With this kind of navigation:

<a href="<?php echo BASE_URL ?>">Home</a>
<a href="index.php?page=about">About</a>
<a href="index.php?page=contacts">Contacts</a>

I read here that this way is not recommended because it is hackable and the switch statement will be very large if the pages are lot.

I was told about this practice: For every page from the project, a real physical file exist like index.php, about.php, contacts.php etc and every one of them includes its content.

And the navigation is like:

<a href="index.php">Home</a>
<a href="about.php">About</a>
<a href="contacts.php">Contacts</a>

Which way is better? If the project has 100 pages do I have to place 100 files? This means my root directory will contains a lot of files. Is there a way I store these files in some folder instead of the root dir of the project?

Thank you very much in advance for your attention and answers!

I looked a lot at CodeIgniter to see how it is managed, but it seems to complicated and understandable for me yet. I think it is something with the .htaccess files and some routings. Is it the way?

Liam James
  • 414
  • 1
  • 6
  • 15
  • I prefere the first one but without a switch statement but a mapping stored in a json / mysql so you don't have to rewrite the switch every time you load a pager – Daniel W. Jan 27 '14 at 09:46
  • DanFromGermany, is this what you are talking about? http://stackoverflow.com/questions/443499/json-to-map – Liam James Jan 27 '14 at 10:00
  • no I mean URL to script mapping (your `switch` is actually kind of mapping, but I mean a more dynamic one) – Daniel W. Jan 27 '14 at 10:12

3 Answers3

1

am assuming you have 90+ files, you can go for dynamic content like:

index.php
content.php

index.php will run normally
content.php will execute all other pages by fetching data from db

you menu would be like

<a href="index.php">Home</a>
<a href="content.php?page=about">About</a>
<a href="content.php?page=contact">Contacts</a>

also you can show the urls as per your choice via .htaccess like

content.php?page=content can be shown as /page.php r /page.html and so on...

i hope you get an idea.

Pranay Bhardwaj
  • 1,059
  • 8
  • 9
1

Main file with parameters is better because you as a programmer have control over what content is loaded at what address. This is what all frameworks do - they fetch the whole path and query parameters using mod_rewrite Apache module and .htaccess file and interpret them according to configuration stored in project code. This is especially useful when you're moving things between parts of your project as you can dynamically say that /contact loads not contact page but (for example) internal client support system.

Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
  • The question is not about how to mask the address, the question is about implementing the routing. – Daniel W. Jan 27 '14 at 10:15
  • The question is "primarily opinion-based" about which way is better. I gave some thoughts about my opinion on subject supported by related technical solutions. – Tomasz Kowalczyk Jan 27 '14 at 16:08
1

The second solution is not common because you'd have to include the whole bootstrapping into every single content script. If you have to change something, you'd have to change it in every single file.

Your first example is prefered but as you said it's very static and uncomfortable for many pages. So you'd like to store this in an array in a file or in the database.

In the db you'd store

current page              script    
home                      home.php
about                     about.php

You fetch the current page ($_GET['page']) and in return have the script to include.

Instead of:

<a href="<?php echo BASE_URL ?>">Home</a>

You should implement

<a href="index.php?page=home">Home</a>

If you want mod_rewrite support or nginx rewrites, you can enforce redirecting /index.php?page=X to /X then have to adjust your stored current page value in database.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151