I'm creating a simple MVC framework for a new version of my website I'm working on. Currently I have simple routing, such as /about-us etc ...
I've just implemented 'my version' of dynamic routing. I have no idea whether or not my implementation is idiotic or not. I made it up as I went along, so I've no idea if this is a horrible way to implement dynamic routing or not.
I'd be grateful if someone could review my implementation and provide some feedback.
Here's 'Routes.php', this stores the user-defined routes.
// This is the index page. The first route.
Route::set('', function() {
View::make('Root');
});
// This is a test route, leading to an about-us page.
Route::set('about-us', function() {
View::make('AboutUs');
});
/*
* This is an example of a dynamic route. In this example,
* this route would display a users profile page.
*/
Route::set('user/<1>', function() {
Route::dyn('user/<1>');
View::make('UserProfile');
});
The method View::make()
has nothing to do with the routing. It's just designed to create the HTML page and the Controller for the page.
The method Route::set()
is how I create all routes. Simple routes are created entirely with Route::set()
. The dynamic routes are created by executing the closure. Route::set()
executes the closure for simple and dynamic routes, for simple routes the closure creates the view and for dynamic routes it swaps <1>
for a parameter from the URL and then creates the view.
The reason the syntax is odd, <1>
, is because if you want multiple dynamic parameters you can use <2>
etc ...
Here's an example
In this example the URL string might look like this user/francis/settings/something-else
Route::set('user/<1>/settings/<2>', function() {
Route::dyn('user/<1>/settings/<2>');
View::make('UserProfile');
});
Here's the implementation of the Route::dyn() method
public static function dyn($dyn_routes) {
$route_components = explode('/', $dyn_routes);
$uri_components = explode('/', substr($_SERVER['REQUEST_URI'], strlen(BASEDIR)-1));
for ($i = 0; $i < count($route_components); $i++) {
if ($i+1 <= count($uri_components)-1) {
$route_components[$i] = str_replace("<$i>", $uri_components[$i+1], $route_components[$i]);
}
}
$route = implode($route_components, '/');
self::registerRoute($route);
}
$route_components
is what we pass to Route::dyn(). $uri_components
is the string we get from the URL, i.e user/francis/settings/something-else
. This function converts $route_components so that it is the same as $uri_components. The reason for doing so is that I add all routes to a global array called $Routes
. When someone visits a page the URL is checked against the valid routes in the $Routes
array.
When the user visits user/francis
I want the method Route::set('user/<1>')
to run so that the profile page is displayed. It all works with my implementation, I'm just wondering if there is a better way.
Thanks, Francis