|
| 1 | +# phpTreeBasedRouter |
| 2 | +The main objective for this Router is to organize the possible routes in a hierarchical way |
| 3 | +although you are free to implement a different organization/data structure such as arrays with one or multiple |
| 4 | +dimensions. |
| 5 | + |
| 6 | +# Brief summary of Objects/Classes |
| 7 | +Here are some objects that will help you initialize your router: |
| 8 | + |
| 9 | +##### Route |
| 10 | +`use MinusFour\Router\Route;` |
| 11 | + |
| 12 | +This object will hold the route, the name of the route and the actions per each method (which can be HTTP Methods, |
| 13 | +but there's really no restriction for that). |
| 14 | + |
| 15 | +##### Action |
| 16 | +`use MinusFour\Router\Action;` |
| 17 | + |
| 18 | +This object holds the Class and Method to be called plus any fixed arguments to passed along. |
| 19 | + |
| 20 | +##### RouteContainer |
| 21 | +`use MinusFour\Router\TreeRouteContainer;` |
| 22 | + |
| 23 | +You'll basically load your route objects into the router container. This object will match a given path to a route |
| 24 | +object. |
| 25 | + |
| 26 | +##### Router |
| 27 | +`use MinusFour\Router\Router;` |
| 28 | + |
| 29 | +Once you are finished done loading objects into the RouteContainer, the Router will pick up the route object and call |
| 30 | +the action according to the supplied method. This object is also responsible for building a path for a given route, |
| 31 | +which at the moment is unfinished. |
| 32 | + |
| 33 | +# How to use |
| 34 | + |
| 35 | +Start by including the autoloader and setting up the classes: |
| 36 | +```php |
| 37 | +include 'src/Autload.php' |
| 38 | + |
| 39 | +use MinusFour\Router\Action; |
| 40 | +use MinusFour\Router\Route; |
| 41 | +use MinusFour\Router\RouteContainer; |
| 42 | +use MinusFour\Router\Router; |
| 43 | +``` |
| 44 | + |
| 45 | +This is how you create a route object: |
| 46 | +```php |
| 47 | +$route = new Route('nameOfRoute', '/'); //Route for '/' |
| 48 | +``` |
| 49 | + |
| 50 | +This is how you assign an action to said route object: |
| 51 | +```php |
| 52 | +$route->setMethodAction('GET', new Action('MyClass', 'MyMethod')); |
| 53 | +//Router will call method `MyMethod` from Object `MyClass` when the '/' route is matched and GET method is called. |
| 54 | +``` |
| 55 | + |
| 56 | +You must then initialize the RouteContainer object and add it. |
| 57 | +```php |
| 58 | +$routeContainer = new TreeRouteContainer(); |
| 59 | +$routeContainer->addRoute($route); |
| 60 | +//Loads the route object into the tree. |
| 61 | +``` |
| 62 | + |
| 63 | +Finall you'll initialize the Router Object with `$routeContainer` and at that point is up to you to supply a path |
| 64 | +to match a Route object. |
| 65 | + |
| 66 | +```php |
| 67 | +$router = new Router($routeContainer); |
| 68 | +$route->dispatch($_SERVER['REQUEST_METHOD'], strtok($_SERVER['REQUEST_URI'], '?')); |
| 69 | +//This uses the HTTP method and the HTTP URI as arguments for the router. |
| 70 | +//I.e. GET /home?section=1 |
| 71 | +//Will use GET as the Method and /home for path. |
| 72 | +``` |
| 73 | + |
| 74 | +# Regular Expresions in Routes |
| 75 | + |
| 76 | +It is possible to use regular expressions in some parts of the route. For example: |
| 77 | + |
| 78 | +`$route = new Route('regex_route_example', '/section:(home|news)');` |
| 79 | + |
| 80 | +It will match /home or /news. |
| 81 | + |
| 82 | +It's possible to mix in static elements as well. |
| 83 | + |
| 84 | +`$route = new Route('regex_route_example_2', '/section/name:(home|news)');` |
| 85 | + |
| 86 | +Will match /section/home o /section/news. |
| 87 | + |
| 88 | +# Warnings about path elements using regular expressions |
| 89 | + |
| 90 | +#### General Note |
| 91 | + |
| 92 | +You can have multiple expressions in one path, but keep in mind that the elements of the path are being tested |
| 93 | +not the path itself. |
| 94 | + |
| 95 | +#### No slash |
| 96 | + |
| 97 | +You shouldn't use a slash in your expressions. |
| 98 | + |
| 99 | +`$route = new Route('regex_route_bad_example', '/section:(home/news|home/forums)');` |
| 100 | + |
| 101 | +Will not work. It will create static childrens for news|home and forums and the first element will be tested for (home. |
| 102 | + |
| 103 | +#### Multiple matches restriction |
| 104 | + |
| 105 | +If you have expressions that match the same thing only one of them will be matched. |
| 106 | +Let say for an instance that you have routes like this: |
| 107 | + |
| 108 | +```php |
| 109 | +$route = new Route('regex_common_route', '/route:\w+'); //Matches alphanumerical characters. |
| 110 | +$route2 = new Route('regex_common_route_2', '/route2:\d+'); //Matches numbers. |
| 111 | +``` |
| 112 | + |
| 113 | +And the path to be matched is: /123456. Only the first one will be matched, it will not even attempt to try out |
| 114 | +the second expression as there's already one that fits plus there's no way the Router could now the other one |
| 115 | +is the valid one, even though is a more restrictive expression. |
| 116 | + |
| 117 | +## HOWEVER |
| 118 | + |
| 119 | +If the matched expresion fails to continue down its hierarchical path, it will attempt to use any other expression |
| 120 | +available. |
| 121 | + |
| 122 | +```php |
| 123 | +$route = new Route('regex_common_route', '/route:\w+'); //Matches alphanumerical characters. |
| 124 | +$route2 = new Route('regex_common_route_2', '/route2:\d+\static'); //Matches numbers. |
| 125 | +``` |
| 126 | + |
| 127 | +It will behave like this: |
| 128 | + |
| 129 | +``` |
| 130 | +/123456/static -> regex_common_route_2 |
| 131 | +/123456 -> regex_common_route |
| 132 | +``` |
| 133 | + |
| 134 | +# About 404 Error Codes |
| 135 | + |
| 136 | +When the router fails to match a Route, it will throw a `RouteNotFoundException`. If it fails to find an action for |
| 137 | +the specified method, it will throw a `MethodNotFoundException`. Both can be caught under a `NotFoundException`. |
| 138 | + |
| 139 | +`RouteNotFoundException` and `MethodNotFoundException` both live under `/MinusFour/Router` while `NotFoundException` |
| 140 | +is under `MinusFour\Utils`. |
0 commit comments