platform/packages/route
2022-03-19 09:37:28 +08:00
..
example Publish angel3_route 2021-05-14 15:02:50 +08:00
lib Updated to 6.0.0 2022-02-14 00:48:37 +08:00
test Removed redundant code 2021-06-26 21:13:43 +08:00
web Publish angel3_route 2021-05-14 15:02:50 +08:00
.gitignore Publish angel3_route 2021-05-14 15:02:50 +08:00
.travis.yml Add 'packages/route/' from commit '8b2b0524609ab737eb5569fdf82a7ac491fdc67b' 2020-02-15 18:22:17 -05:00
analysis_options.yaml Updated linter to package:lints 2021-09-25 14:32:32 +08:00
AUTHORS.md Publish angel3_route 2021-05-14 15:02:50 +08:00
CHANGELOG.md Updated to 6.0.0 2022-02-14 00:48:37 +08:00
LICENSE Updated linter to package:lints 2021-09-25 14:32:32 +08:00
melos_angel3_route.iml Added melos 2022-03-19 09:37:28 +08:00
pubspec.yaml Updated to 6.0.0 2022-02-14 00:48:37 +08:00
README.md Updated to 6.0.0 2022-02-14 00:48:37 +08:00
repubspec.yaml Add 'packages/route/' from commit '8b2b0524609ab737eb5569fdf82a7ac491fdc67b' 2020-02-15 18:22:17 -05:00

Angel3 Route

Pub Version (including pre-releases) Null Safety Gitter License

A powerful, isomorphic routing library for Dart.

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 Angel 3, 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 Angel framework.

Contents

Examples

Routing

If you use Angel3, every Angel instance is a Router in itself.

void main() {
  final router = Router();
  
  router.get('/users', () {});
  
  router.post('/users/:id/timeline', (String id) {});
  
  router.get('/square_root/:id([0-9]+)', (n) { 
    return { 'result': pow(int.parse(n), 0.5) };
  });
  
  // You can also have parameters auto-parsed.
  //
  // Supports int, double, and num.
  router.get('/square_root/int:id([0-9]+)', (int n) { 
      return { 'result': pow(n, 0.5) };
    });
  
  router.group('/show/:id', (router) {
    router.get('/reviews', (id) {
      return someQuery(id).reviews;
    });
    
    // Optionally restrict params to a RegExp
    router.get('/reviews/:reviewId([A-Za-z0-9_]+)', (id, reviewId) {
      return someQuery(id).reviews.firstWhere(
        (r) => r.id == reviewId);
    });
  }, middleware: [put, middleware, here]);

  // Grouping can also take async callbacks.
  await router.groupAsync('/hello', (router) async {
    var name = await getNameFromFileSystem();
    router.get(name, (req, res) => '...');
  });
}

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

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 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

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:

var router = Router();
router.get('/book/:id/authors', () => ...);

var result = router.resolve('/book/foo/authors');
var params = result.allParams; // {'id': 'foo'};