`angel3_route` exposes a routing system that takes the shape of a tree. This tree structure can be easily navigated, in a fashion somewhat similar to a filesystem. The `Router` API is a very straightforward interface that allows for your code to take a shape similar to the route tree. Users of Laravel and Express will be very happy.
`angel3_route` does not require the use of [Protevus 3](https://pub.dev/packages/angel3_framework), and has minimal dependencies. Thus, it can be used in any application, regardless of framework. This includes Web apps, Flutter apps, CLI apps, and smaller servers which do not need all the features of the Protevus framework.
The default `Router` does not give any notification of routes being changed, because there is no inherent stream of URL's for it to listen to. This is good, because a server needs a lot of flexibility with which to handle requests.
### Hierarchy
```dart
void main() {
final router = Router();
router
.chain('middleware1')
.chain('other_middleware')
.get('/hello', () {
print('world');
});
router.group('/user/:id', (router) {
router.get('/balance', (id) async {
final user = await someQuery(id);
return user.balance;
});
});
}
```
See [the tests](test/route/no_params.dart) for good examples.
## In the Browser
Supports both hashed routes and pushState. The `BrowserRouter` interface exposes a `Stream<RoutingResult> onRoute`, which can be listened to for changes. It will fire `"NULL"` whenever no route is matched.
`angel3_route` will also automatically intercept `<a>` elements and redirect them to your routes.
To prevent this for a given anchor, do any of the following:
- Do not provide an `href`
- Provide a `download` or `target` attribute on the element
- Set `rel="external"`
## Route State
```dart
main() {
final router = BrowserRouter();
// ..
router.onRoute.listen((route) {
if (route == null)
throw 404;
else route.state['foo'] = 'bar';
});
router.listen(); // Start listening
}
```
For applications where you need to access a chain of handlers, consider using `onResolve` instead. You can see an example in `web/shared/basic.dart`.
## Route Parameters
Routes can have parameters, as seen in the above examples. Use `allParams` in a `RoutingResult` to get them as a nice `Map`: