├── .htaccess ├── index.php ├── RegexRouter.php ├── README.md └── LICENSE /.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On 2 | RewriteCond %{REQUEST_FILENAME} !-f 3 | RewriteCond %{REQUEST_FILENAME} !-d 4 | RewriteRule ^(.*)$ index.php [QSA,L] -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | route('/^\/blog\/(\w+)\/(\d+)\/?$/', function($category, $id){ 7 | print "category={$category}, id={$id}"; 8 | }); 9 | $router->execute($_SERVER['REQUEST_URI']); -------------------------------------------------------------------------------- /RegexRouter.php: -------------------------------------------------------------------------------- 1 | routes[$pattern] = $callback; 9 | } 10 | 11 | public function execute($uri) { 12 | foreach ($this->routes as $pattern => $callback) { 13 | if (preg_match($pattern, $uri, $params) === 1) { 14 | array_shift($params); 15 | return call_user_func_array($callback, array_values($params)); 16 | } 17 | } 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | RegexRouter 2 | =========== 3 | 4 | PHP class to route with regular expressions. Extremely small. Follows every conceivable best-practice - SRP, SoC, DI, IoC, bfft... 5 | 6 | Usage 7 | =========== 8 | 9 | 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. 10 | 11 | Setup 12 | =========== 13 | 14 | 1. make sure you're sending all requests to a front controller (either through apache conf directly or htaccess) 15 | 1. include or require RegexRouter.php `require_once 'RegexRouter.php';` 16 | 1. instantiate a new instance `$router = new RegexRouter();` 17 | 1. add some routes `$router->route('/^\/some\/pattern$/', );` 18 | 1. pass it either REQUEST_URI or any string for unit testing `$router->execute($_SERVER['REQUEST_URI']);` 19 | 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | --------------------------------------------------------------------------------