Skip to content

bencrowder/router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Router

Router is a (very) small PHP library for doing URL routing.

Installation

  1. Copy Router.class.php somewhere in your project directory.
  2. Include it (via require_once or include).
  3. Put the code from the htaccess section into your .htaccess file (or rename the included htaccess file to .htaccess):
  4. Modify index.php as necessary.

htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php	[L]

Usage

Usage looks like this:

$url = $_SERVER["REQUEST_URI"];

$routes = array(
	'#^/app/([^/]+)/?$#' => 'appHandler',
	'#^/(\d\d\d\d)/(\d\d)/(\d\d)/([^/]+)/?$#' => 'blogHandler'
);

We've set up the array of handlers, using # as our regex delimiter so we don't have to escape forward slashes. The value part of each pair is the name of the handler function you want called when the associated regex matches the URL. You can use plain function names or make them static functions in a class.

Router::route($url, $routes, 'defaultHandler');

This does the routing. You pass in the string to parse (the URL), the array of routes, and the name of the default handler.

function appHandler($args) {
	echo $args[0];
}

function blogHandler($args) {
	$year = $args[0];
	$month = $args[1];
	$day = $args[2];
	$slug = $args[3];

	echo "Blog post date: $month/$day/$year<br>";
	echo "Slug: $slug";
}

function defaultHandler() {
	echo "Default";
}

And these are the (very simplistic) handlers.

A working example is in the included index.php file.

About

Small PHP library for doing URL routing. Not maintained anymore.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages