1
1
# phpTreeBasedRouter
2
- The main objective for this Router is to organize the possible routes in a hierarchical way
2
+ The main objective for this Router is to organize the possible routes in a hierarchical way
3
3
although you are free to implement a different organization/data structure such as arrays with one or multiple
4
4
dimensions.
5
5
@@ -9,7 +9,7 @@ Here are some objects that will help you initialize your router:
9
9
##### Route
10
10
` use MinusFour\Router\Route; `
11
11
12
- This object will hold the route, the name of the route and the actions per each method (which can be HTTP Methods,
12
+ This object will hold the route, the name of the route and the actions per each method (which can be HTTP Methods,
13
13
but there's really no restriction for that).
14
14
15
15
##### Action
@@ -108,7 +108,7 @@ Will not work. It will create static childrens for news|home, forums) and the fi
108
108
109
109
#### Multiple matches restriction
110
110
111
- If you have expressions that match the same thing only one of them will be matched.
111
+ If you have expressions that match the same thing only one of them will be matched.
112
112
Let say for an instance that you have routes like this:
113
113
114
114
``` php
@@ -137,6 +137,64 @@ It will behave like this:
137
137
/123456 -> regex_common_route
138
138
```
139
139
140
+ # Route Loading
141
+
142
+ I have included an Implementation of a possible Json Route Loader. It basically reads a file with a Json object, parses it,
143
+ builds their respective route objects and finally loads them into the Router. JSON example:
144
+
145
+ ``` js
146
+ {
147
+ " home_route" : {
148
+ " path" : " /" ,
149
+ " actions" : {
150
+ " GET" : {
151
+ " class" : " MyClass" ,
152
+ " method" : " MyMethod"
153
+ }
154
+ }
155
+ }
156
+ }
157
+ ```
158
+
159
+
160
+ ``` php
161
+ $routeContainer = new TreeRouteContainer();
162
+ $routeLoader = new JsonRouteLoader(['routes.json'], __DIR__);
163
+ $routeLoader->loadRoutes($routeContainer);
164
+ //$routeContainer now holds all the routes on routes.json
165
+ ```
166
+
167
+ It's also possible to load multiple files:
168
+ ``` php
169
+ $routeLoader = new JsonRouteLoader(['routes.json', 'routes2.json'], __DIR__);
170
+ ```
171
+
172
+ You can also delegate routes to other files under a common path:
173
+ ``` js
174
+ {
175
+ " delegate" : {
176
+ " path" : " /delegate" ,
177
+ " include" : " /include.json"
178
+ }
179
+ }
180
+ ```
181
+
182
+ Will add all routes in include.json under /delegate. Please avoid delegating root paths.
183
+
184
+ # Url Building
185
+
186
+ This barely builds a path. It will fetch the route object and the path associated. It's up to you to build the full url:
187
+
188
+ ``` php
189
+ //After the Router object has been instantiated with its respective TreeRouteContainer:
190
+ $router->buildUrl('name_of_route', array('parameterName' => 'parameterValue));
191
+ // Parameter names must match the name of the elements given on the path. I.e. /name:(alex|john)/section:(home|news)
192
+ // name and section are both parameter names. Therefore, the supplied array can be:
193
+ // array('name' => 'alex', 'section' => 'home');
194
+ // Which will return:
195
+ // /alex/home
196
+ ```
197
+
140
198
# About 404 Error Codes
141
199
142
200
When the router fails to match a Route, it will throw a ` RouteNotFoundException ` . If it fails to find an action for
0 commit comments