Well... there are many framework in internet for php routing. If you want you can try that from https://packagist.org/search/?q=route. But to be honest, if you are from procedural PHP world, it could be problematic for you first time.
If you like, you can use following code written by Jesse Boyer that I used to for my own projects. You application structure should follows-
[Application-folder]
- index.php
- .htaccess
- route.php
route.php
<?php
/**
* @author Jesse Boyer <contact@jream.com>
* @copyright Copyright (C), 2011-12 Jesse Boyer
* @license GNU General Public License 3 (http://www.gnu.org/licenses/)
* Refer to the LICENSE file distributed within the package.
*
* @link http://jream.com
*
* @internal Inspired by Klein @ https://github.com/chriso/klein.php
*/
class Route
{
/**
* @var array $_listUri List of URI's to match against
*/
private static $_listUri = array();
/**
* @var array $_listCall List of closures to call
*/
private static $_listCall = array();
/**
* @var string $_trim Class-wide items to clean
*/
private static $_trim = '/\^$';
/**
* add - Adds a URI and Function to the two lists
*
* @param string $uri A path such as about/system
* @param object $function An anonymous function
*/
static public function add($uri, $function)
{
$uri = trim($uri, self::$_trim);
self::$_listUri[] = $uri;
self::$_listCall[] = $function;
}
/**
* submit - Looks for a match for the URI and runs the related function
*/
static public function submit()
{
$uri = isset($_REQUEST['uri']) ? $_REQUEST['uri'] : '/';
$uri = trim($uri, self::$_trim);
$replacementValues = array();
/**
* List through the stored URI's
*/
foreach (self::$_listUri as $listKey => $listUri)
{
/**
* See if there is a match
*/
if (preg_match("#^$listUri$#", $uri))
{
/**
* Replace the values
*/
$realUri = explode('/', $uri);
$fakeUri = explode('/', $listUri);
/**
* Gather the .+ values with the real values in the URI
*/
foreach ($fakeUri as $key => $value)
{
if ($value == '.+')
{
$replacementValues[] = $realUri[$key];
}
}
/**
* Pass an array for arguments
*/
call_user_func_array(self::$_listCall[$listKey], $replacementValues);
}
}
}
}
.htaccess
here in 2nd line, instead of '/php/cfc/' you need to put your localhost project directory name, such as 'Application-folder'
RewriteEngine On
RewriteBase /php/cfc/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
index.php
In index.php file, you need to write following codes-
<?php
include "route.php";
/**
* -----------------------------------------------
* PHP Route Things
* -----------------------------------------------
*/
//define your route. This is main page route. for example www.example.com
Route::add('/', function(){
//define which page you want to display while user hit main page.
include('myindex.php');
});
// route for www.example.com/join
Route::add('/join', function(){
include('join.php');
});
Route::add('/login', function(){
include('login.php');
});
Route::add('/forget', function(){
include('forget.php');
});
Route::add('/logout', function(){
include('logout.php');
});
//method for execution routes
Route::submit();
This things work perfectly for me. Hope it will work for you too...
Happy Coding... :)