I am trying to load my whole site through a single point of entry, index.php?uri
My goal is to have nice clean urls though, so far I have come up with...
domain.com/section-name/page-of-that-section
so the first part will load the class/file for the section (mail, users, account, etc.)
The 2nd part will load the class/page for the page of that section
This is pretty straight forward and easy so far. The part that is tricky for me is that some pages will have an ID number (user ID, message ID, blog ID, forum ID, forum post ID, forum topic ID, etc.) this will be used to look up that record in the database by id number.
Next, some pages will also have a page number used for paging.
So sometimes a page number will be present and sometime it will not, same with the id numbers depending on the section it is in.
I am looking for examples, advice, help on getting the page numbers and id numbers part working, I need to be able to get them and assign them to a variable $page_number and $id_number
Below is my code I have so far along with some example url's that I will need to access. I do not want to use an existing framework for this, please help if you can I think I am getting close to getting this to work.
right now I have added id/ in front of id numbers and page/ in front of a paging number, this could be changed to anything (ie; id-423423 page-23 page/23 page23 p/23) as long as I can get the numbers to store to a variable in the PHP
<?php
//get uri
$uri = isset( $_GET['uri'] ) ? $_GET['uri'] : null;
//put uri into array, everything in between '/' will be an array value
$uri1 = explode('/', $uri);
//the first array value will be the SECTION name/class
$section_name = $uri['0'];
//the second array value will always be the PAGE name/class
$page_name = $uri['1'];
//Now some pages will have numbers in the uri
// some are id numbers for things such as a message ID, users ID, comment ID, photo ID, blog ID
// then to complicate it even more sometimes, pages will need paging so will also have a page number in the uri
// I am looking for a way to detect if there is a ID number or page number in uri's for example like listed below.
// right now I have added id/ in front of id numbers and page/ in front of a paging number, this could be changed to anything **(ie; id-423423 page-23 page/23 page23 p/23)**
mail/inbox/
mail/inbox/page/12
mail/message/id/3432435
users/online/
users/online/page/234
users/profile/id/42234
users/comments/id/42234
users/comments/id/42234/page/3
blogs/list/id/42234
blogs/list/id/42234/page/25
blogs/viewpost/id/1323/
blogs/create/
blogs/edit/id/34453353
?>
The easiest way is to create a routing system, using regular expressions it's quite an easy task. You can also use some opensource stuff:
http://robap.github.com/php-router/
And here is mine for example (simple as a bottle of vodka, but working):
// for urls like http://mysite.com/1 or http://mysite.com/29 (any digit)
$ROUTE['(\d*)'] = 'pages/showPage/$1' ;
// for url http://mysite.com/admin/new-page
$ROUTE['admin\/new-page'] = 'pages/addPage' ;
function get_route($uri) {
global $ROUTE ;
$routes = $ROUTE ;
foreach ($routes as $rUri => $rRoute) {
if (preg_match("#^{$rUri}$#Ui", $uri)) {
$route = preg_replace("#^{$rUri}$#Ui", $rRoute, $uri) ;
break ;
}
}
$route = explode('/', $route) ;
$return['controller'] = array_shift($route) ;
$return['action'] = array_shift($route) ;
$return['params'] = empty($route) ? array() : $route ;
return $return ;
}
//testing for http://mysite.com/2
$route = get_route( $_GET[ 'uri' ] ) ;
var_dump( $route ) ;