tags:

views:

19

answers:

1

I dont know if this is a great idea...or a crap one. But I was thinking I may use one page to display all my pages using includes. Here is what my index.php would look like...on the functions include there is a function called "superSwitch" which will determine what requested page will be included....for instance

if I do a get ?a=a

it will goto the function superSwitch(a) superSwitch will take it and associate it with (login.php) then respond with such...

here is the code for the index.php...please let me know if this makes sense and might work, or should I just stick to long blocks of code (which is why I am trying this because I hate long pages full of code...)

of course as you can tell it is not actually including anything yet...the print is for debugging purposes. :)

Thanks, Matt

<?php
//includes Functions
include_once('inc/func.inc.php');
//set superget variable
$superget = @$_GET['a'];
//check if superget is set or null
if (!$superget) 
{
    echo "Nothing Requested :)";
}
else
{
    //sanitizes the superget request
    $supergetr = supergetSanitize($superget);
    //uses the result "good" or "nogood" to determine what happens
    if ( $supergetr == "good" ) {
        //pulls superSwitch value of the request
        $ssresult = superSwitch($superget);
        print_r ($ssresult);
    }
    //if the sanitize is nogood
    else
    {
        //the superSwitch is instructed to respond with a 404 page
        $superget = "404";
        $ssresult = superSwitch($superget);
        print_r ($ssresult);
    }
}
?>
+1  A: 

You have just reinvented the front controller design pattern :) In practice, you have one single page, which handles all the request. Usually, you will use apache rewrite rules to mask this, and have clean urls. This makes sense, and is a good idea, especially if you intend using the MVC pattern.

The actual implementation is up to you. The one you are proposing is very simplicistic, but can be a good start. If you look at famous CMSes like Drupal, you'll find this at the core. You may also want to take a look at the (too complex, IMHO) Zend Framework Page Controller.

As a bonus, put this in your .htaccess, and you'll have any page redirected to index.php?q=your/query/here. (E.g. http://www.yoursite.com/here/I/go?hi=1 will be passed to index.php?q=here/I/go&hi=1). Courtesy of Drupal.

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Palantir
Haha, thats great! I never thought I would do anything that was worth a compliment.Thank you very much for that.I am kind of confused on the apache rewriteengine...would this handle all requests anywhere on my site? and automatically (without 404) redirect them?Please elaborate :) I would love to know more
Matt
Yes, you should try that yourself and experiment. Put that code in a text file called .htaccess (note the dot in front, and no extension) in the root of your site. Then print_r($_GET) at the top of your index.php page, to see what you are getting. Of course, I am assuming you are using apache and that you can use the overriding rules, which is a pretty standard setup.
Palantir