Skip to content

Commit 1277776

Browse files
committed
Update README.md with new additions
1 parent c12417e commit 1277776

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

README.md

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# 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
33
although you are free to implement a different organization/data structure such as arrays with one or multiple
44
dimensions.
55

@@ -9,7 +9,7 @@ Here are some objects that will help you initialize your router:
99
##### Route
1010
`use MinusFour\Router\Route;`
1111

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,
1313
but there's really no restriction for that).
1414

1515
##### Action
@@ -108,7 +108,7 @@ Will not work. It will create static childrens for news|home, forums) and the fi
108108

109109
#### Multiple matches restriction
110110

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.
112112
Let say for an instance that you have routes like this:
113113

114114
```php
@@ -137,6 +137,64 @@ It will behave like this:
137137
/123456 -> regex_common_route
138138
```
139139

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+
140198
# About 404 Error Codes
141199

142200
When the router fails to match a Route, it will throw a `RouteNotFoundException`. If it fails to find an action for

0 commit comments

Comments
 (0)