├── .gitignore ├── .htaccess ├── API ├── api.php ├── route.php └── router.php ├── README.md └── index.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On 2 | RewriteCond %{REQUEST_FILENAME} !-f 3 | RewriteRule ^(.*)$ index.php [QSA,L] 4 | -------------------------------------------------------------------------------- /API/api.php: -------------------------------------------------------------------------------- 1 | _URI = $this->_checkKey('URI', $inputs); 17 | $this->_rawInput = $this->_checkKey('raw_input', $inputs); 18 | $this->_method = $this->_checkKey('method', $inputs); 19 | } 20 | 21 | //Return NULL if the key does not exist 22 | private function _checkKey($key, $array){ 23 | return array_key_exists($key, $array) ? $array[$key] : NULL; 24 | } 25 | 26 | public function run() { 27 | 28 | //Create the router 29 | $router = new Router(); 30 | 31 | // Populate the router 32 | 33 | // GET homepage 34 | $router->addRoute('GET', '/', function() { 35 | echo "Home page"; 36 | }); 37 | 38 | // GET info page on a specific date 39 | $router->addRoute('GET', '/info/:day/:month/:year', function($day,$month,$year) { 40 | echo "Infos for $day/$month/$year"; 41 | }); 42 | 43 | // Execute functions 44 | function hello() { echo "Hello "; } 45 | function world() { echo "world "; } 46 | $router->addRoute('GET', '/helloworld' , hello(), world(), function() { 47 | echo "!"; 48 | }); 49 | 50 | //Run the router 51 | $router->run($this->_method, $this->_URI); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /API/route.php: -------------------------------------------------------------------------------- 1 | _method = array_shift($args); 16 | $this->_pattern = array_shift($args); 17 | $this->_function = array_pop($args); 18 | $this->_optionalFunctions = $args; 19 | $this->_param = array(); 20 | } 21 | 22 | public function methodMatches($method) { 23 | if ( $this->_method == $method ) 24 | return true; 25 | else 26 | return false; 27 | } 28 | 29 | public function patternMatches($URI) { 30 | //Extract URL params 31 | preg_match_all('@:([\w]+)@', $this->_pattern, $paramNames, PREG_PATTERN_ORDER); 32 | $paramNames = $paramNames[0]; 33 | 34 | //Convert URL params into regex patterns, construct a regex for this route 35 | $patternAsRegex = preg_replace_callback('@:[\w]+@', array($this, '_convertPatternToRegex'), $this->_pattern); 36 | if ( substr($this->_pattern, -1) === '/' ) { 37 | $patternAsRegex = $patternAsRegex . '?'; 38 | } 39 | $patternAsRegex = '@^' . $patternAsRegex . '$@'; 40 | 41 | //Cache URL params' names and values if this route matches the current HTTP request 42 | if ( preg_match($patternAsRegex, $URI, $paramValues) ) { 43 | array_shift($paramValues); 44 | foreach ( $paramNames as $index => $value ) { 45 | $val = substr($value, 1); 46 | if ( isset($paramValues[$val]) ) { 47 | $this->_param[$val] = urldecode($paramValues[$val]); 48 | } 49 | } 50 | return true; 51 | } 52 | return false; 53 | } 54 | 55 | private function _convertPatternToRegex( $matches ) { 56 | $key = str_replace(':', '', $matches[0]); 57 | return '(?P<' . $key . '>[a-zA-Z0-9_\-\.\!\~\*\\\'\(\)\:\@\&\=\$\+,%]+)'; 58 | } 59 | 60 | public function run() { 61 | 62 | //Run the optional functions 63 | foreach ($this->_optionalFunctions as $function) { 64 | if (is_callable($function)) 65 | call_user_func($function); 66 | } 67 | 68 | //Run the main function 69 | if (is_callable($this->_function)) { 70 | call_user_func_array($this->_function, array_values($this->_param)); 71 | return true; 72 | } 73 | return false; 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /API/router.php: -------------------------------------------------------------------------------- 1 | _routes = array(); 16 | 17 | // Contains the matching routes 18 | $this->_matchingRoutes = array(); 19 | 20 | // Init default route 21 | $this->_defaultRoute = NULL; 22 | } 23 | 24 | public function addRoute() { 25 | $args = func_get_args(); 26 | array_push($this->_routes, new Route($args)); 27 | } 28 | 29 | // If nothing match the default route apply 30 | public function setDefaultRoute($route) { 31 | $this->_defaultRoute = $route; 32 | } 33 | 34 | //Starting the router with the HTTP infos 35 | public function run($method, $URI) { 36 | 37 | //Find the matching methods 38 | $this->_findMatchingMethod($this->_routes, $method); 39 | 40 | //In previous matching routes find the ones matching the pattern 41 | $this->_findMatchingPattern($this->_matchingRoutes, $URI); 42 | 43 | if (count($this->_matchingRoutes) == 0) { 44 | //If no route match 45 | if ( !is_null($this->_matchingRoutes) ) 46 | header('Location: '.$this->_defaultRoute); 47 | 48 | } else { 49 | //Run the matching routes 50 | foreach ($this->_matchingRoutes as $route) { 51 | $route->run(); 52 | } 53 | } 54 | 55 | } 56 | 57 | private function _findMatchingMethod($routes, $method) { 58 | foreach ($routes as $route) { 59 | if ( $route->methodMatches($method) ) 60 | array_push($this->_matchingRoutes, $route); 61 | } 62 | } 63 | 64 | private function _findMatchingPattern($routes, $URI) { 65 | //Reset the matching pattern array 66 | $this->_matchingRoutes = array(); 67 | foreach ($routes as $route) { 68 | if ($route->patternMatches($URI)) 69 | array_push($this->_matchingRoutes, $route); 70 | } 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-router 2 | **** 3 | 4 | Simple RESTful php router inpired by Slim framework 5 | 6 | # Files 7 | * .htaccess - Redirect everything to the app 8 | * input.php - The HTTP headers and start the app 9 | * API/api.php - The API with all the routes 10 | * API/router.php - Router class 11 | * API/route.php - Route class 12 | 13 | To add features to the app, you only have to edit the `api` file. 14 | 15 | # Add a route 16 | router = new router(); 17 | router->addRoute(@method, @pattern, [@optional_functions], @function); 18 | router->run(); 19 | 20 | ## Method 21 | The method is a string. 22 | It should be GET, POST, PUT, DELETE 23 | 24 | ## Pattern 25 | You can do simple things like getting a page : 26 | 27 | $router->addRoute('GET', '/', function() { 28 | header('Location: index.html'); 29 | }); 30 | 31 | You can use arguments : 32 | 33 | $router->addRoute('GET', '/info/:day/:month/:year', function($day,$month,$year) { 34 | echo "Infos for $day/$month/$year"; 35 | }); 36 | 37 | ## Optional functions 38 | You can add optional function, to check authentication for exemple. 39 | 40 | function hello() { echo "Hello "; } 41 | function world() { echo "world "; } 42 | $router->addRoute('GET', '/helloworld' , hello(), world(), function() { 43 | echo "!"; 44 | }); 45 | 46 | # Main function 47 | You can do basic stuff as showned before. You can also use objects: 48 | 49 | $router->addRoute('GET', '/datas', function() use ($db, $message, $session) { 50 | $entries = $db->lastestEntries($session->getUserid()); 51 | $message->setData($entries); 52 | $message->send(); 53 | }); 54 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | run(); --------------------------------------------------------------------------------