patternphpMinor
MVC router class
Viewed 0 times
routerclassmvc
Problem
Below is what I have come up with for a router/dispatcher system for my personal framework I am working on. Can you please review and tell me any improvements that could be made?
The first part is an array of URI -> to class/method/id_number/page_number using regex. I have only included a partial list of routes, there will be at least 50 possible routes that will have to run the regex on. I am thinking that is pretty bad for performance, but it seems the best way I know of to do it, since I need to match page numbers and id numbers when they exists.
I am new to MVC so this is my first attempt and I am sure you guys can give me improvement on this, thanks for any tips or help!
Map array():
Router class that reads and matches the Map array above
```
/**
* Run URI against our Map array to get class/method/id-page numbers
*/
class Router
{
private
The first part is an array of URI -> to class/method/id_number/page_number using regex. I have only included a partial list of routes, there will be at least 50 possible routes that will have to run the regex on. I am thinking that is pretty bad for performance, but it seems the best way I know of to do it, since I need to match page numbers and id numbers when they exists.
I am new to MVC so this is my first attempt and I am sure you guys can give me improvement on this, thanks for any tips or help!
.htaccess file:RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?uri=$1 [NC,L,QSA]Map array():
/**
* Map URI to class/method and ID and Page numbers
* Must be an array
*/
$uri_route_map = array(
//forums
'forums/' => array(
'controller' => 'forums',
'method' => 'index',
'id_number' => '',
'page_number' => ''),
'forums/viewforum/(?\d+)' => array(
'controller' => 'forums',
'method' => 'viewforum',
'id_number' => isset($id_number),
'page_number' => ''),
'forums/viewthread/(?\d+)' => array(
'controller' => 'forums',
'method' => 'viewthread',
'id_number' => isset($id_number),
'page_number' => ''),
'forums/viewthread/(?\d+)/page-(?\d+)' => array(
'controller' => 'forums',
'method' => 'viewthread',
'id_number' => isset($id_number),
'page_number' => isset($page_number)),
// user routes
// account routes
// blog routes
// mail routes
// various other routes
);Router class that reads and matches the Map array above
```
/**
* Run URI against our Map array to get class/method/id-page numbers
*/
class Router
{
private
Solution
If you can limit your URI structure to using a delimiter -
Here's a rough example (not with any configuration, but shows the concept):
I'd take a look at the router implementations of some popular frameworks (my recommendation would be ZF's router, but that's just me) - there's nothing wrong from learning how someone else tackled the same problem.
/ comes to mind - you could avoid the regex.Here's a rough example (not with any configuration, but shows the concept):
$uri = 'forums/viewforum/1';
$parts = explode('/', $uri);
$controller = $parts[0];
$method = $parts[1];
$id = parts[2];I'd take a look at the router implementations of some popular frameworks (my recommendation would be ZF's router, but that's just me) - there's nothing wrong from learning how someone else tackled the same problem.
Code Snippets
$uri = 'forums/viewforum/1';
$parts = explode('/', $uri);
$controller = $parts[0];
$method = $parts[1];
$id = parts[2];Context
StackExchange Code Review Q#4031, answer score: 7
Revisions (0)
No revisions yet.