I'm creating a Q &lifier An internet site use php. This is actually the directory structure:


I wish to use index.php 's REQUEST_STRING to load a template.

For instance: index.php?page=signin will load the function get_signin_template() in template.php. And can show the customer a URL like http://example.com/signin/.

Listed here are my questions:

  1. How you can load different templates in index.php (what must do within this file)?
  2. How you can rewrite the url (using apache mod_rewrite)?

Response to Question 1

$valid_templates = array('signin', 'logout', 'home');
   and in_array($_GET['page'], $valid_templates)) {
    $function_name = 'get_' . $_GET['page'] . '_template';
    $template = call_user_func($function_name);

See call_user_func() within the PHP manual and also the in_array() function within the PHP manual.

It can also be worth mentioning which i wouldn't get it done by doing this myself. Mapping Web addresses straight to functions is definitely an inflexible method of doing things.

The main reason I've added the $valid_templates array is to guarantee the user does not make an effort to alter the URL so that they can call a nonexistant function etc. You should add each page/template function into it.

Should you enjoy by doing this of working then I would suggest you take a look at Limonade because it utilizes a similar dispatch method because the one you're trying to create (the next example comes from their official site):

require_once 'vendors/limonade.php';
dispatch('/', 'hello');
  function hello()
      return 'Hello world!';

But is a touch safer and simpler to handle for me. Plus all of the "hard" plumbing work continues to be done and you will just concentrate on adding your website.

Another similar choice is Slim, which appears like this:

require 'Slim/Slim.php';
Slim::get('/hello/:name', function ($name) {
    echo "Hello $name";

Example from their site.

Response to Question 2

Something along wrinkles inside your .htaccess file should work with your spinning needs.

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

1.for index.php?page=signin

require('path_to_file'.$urlEnd);//Require your file
header('Location: index.php?/$urlEnd');//WARNING!!! HEADER MUST go befor any echo's