Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
RewriteEngine On | ||
RewriteCond %{REQUEST_FILENAME} !-f | ||
RewriteCond %{REQUEST_FILENAME} !-d | ||
RewriteRule ^(.*)$ index.php [QSA,L] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,20 @@ | ||
RegexRouter | ||
=========== | ||
|
||
PHP class to route with regular expressions. Extremely small. | ||
PHP class to route with regular expressions. Extremely small. Follows every conceivable best-practice - SRP, SoC, DI, IoC, bfft... | ||
|
||
Usage | ||
=========== | ||
|
||
The only actual code is RegexRouter.php. index.php and the .htaccess file are just demoing usage. The 3 together in a TLD will function. | ||
|
||
Setup | ||
|
||
=========== | ||
|
||
1. make sure you're sending all requests to a front controller (either through apache conf directly or htaccess) | ||
1. include or require RegexRouter.php `require_once 'RegexRouter.php';` | ||
1. instantiate a new instance `$router = new RegexRouter();` | ||
1. add some routes `$router->route('/^\/some\/pattern$/', <closure>);` | ||
1. pass it the uri (either REQUEST_URI or any string for unit testing) `$router->execute($_SERVER['REQUEST_URI']);` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
class RegexRouter { | ||
|
||
private $routes = array(); | ||
|
||
public function route($pattern, $callback) { | ||
$this->routes[$pattern] = $callback; | ||
} | ||
|
||
public function execute($uri) { | ||
foreach ($this->routes as $pattern => $callback) { | ||
if (preg_match($pattern, $uri, $params) === 1) { | ||
array_shift($params); | ||
return call_user_func_array($callback, array_values($params)); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Usage... | ||
* $router = new RegexRouter(); | ||
* $router->route('/^blog\/(\w+)\/(\d+)\/?$/', function($category, $id){ | ||
print "category={$category}, id={$id}"; | ||
}); | ||
* $router->execute($_SERVER['REQUEST_URI']); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
require_once 'RegexRouter.php'; | ||
|
||
$router = new RegexRouter(); | ||
$router->route('/^\/blog\/(\w+)\/(\d+)\/?$/', function($category, $id){ | ||
print "category={$category}, id={$id}"; | ||
}); | ||
$router->execute($_SERVER['REQUEST_URI']); |